diff --git a/error_utils.go b/error_utils.go index 30c6197..bb5088a 100644 --- a/error_utils.go +++ b/error_utils.go @@ -166,16 +166,22 @@ func FieldPathString(path *validate.FieldPath) string { return result.String() } -// markViolationForKey marks the provided error as being for a map key, by -// setting the `for_key` flag on each violation within the validation error. -func markViolationForKey(err error) { +// markViolationForMapEntry marks the provided error as being for a map entry, +// by setting the `for_key` flag on each violation within the validation error +// as well as the field descriptor. +func markViolationForMapEntry(err error, forKey bool, fieldDescriptor protoreflect.FieldDescriptor) { if err == nil { return } var valErr *ValidationError if errors.As(err, &valErr) { for _, violation := range valErr.Violations { - violation.Proto.SetForKey(true) + violation.Proto.SetForKey(forKey) + if forKey { + violation.FieldDescriptor = fieldDescriptor.MapKey() + } else { + violation.FieldDescriptor = fieldDescriptor.MapValue() + } } } } diff --git a/map.go b/map.go index 70c4ec7..ec64869 100644 --- a/map.go +++ b/map.go @@ -109,13 +109,14 @@ func (m *kvPairs) Evaluate(msg protoreflect.Message, val protoreflect.Value, cfg func (m *kvPairs) evalPairs(msg protoreflect.Message, key protoreflect.MapKey, value protoreflect.Value, cfg *validationConfig) (err error) { evalErr := m.KeyRules.EvaluateField(msg, key.Value(), cfg, true) - markViolationForKey(evalErr) + markViolationForMapEntry(evalErr, true, m.Descriptor) ok, err := mergeViolations(err, evalErr, cfg) if !ok { return err } evalErr = m.ValueRules.EvaluateField(msg, value, cfg, true) + markViolationForMapEntry(evalErr, false, m.Descriptor) _, err = mergeViolations(err, evalErr, cfg) return err }