fix(validate): do not panic with field errors on non-struct types

When validating non-struct types, we should not try and convert field
values in errors, because there is no struct field to lookup. Hence this
fix stops a panic from happening.

It is up to the Validate() method itself to provide the correct and
final field value when validating non-struct types.
This commit is contained in:
2021-08-23 12:55:24 +01:00
parent 629f13e8aa
commit 9c3b63361d
2 changed files with 50 additions and 1 deletions

View File

@@ -83,7 +83,7 @@ func (s *Validator) validate(path []string, data interface{}) error {
e := &Error{}
if ok := errors.As(err, &e); ok {
field := e.Field
if field != "" {
if field != "" && d.Kind() == reflect.Struct {
if sf, ok := d.Type().FieldByName(e.Field); ok {
field = s.fieldName(sf)
}