Fix map diving validation (#793)

pull/787/merge
Alonso Villegas 3 years ago committed by GitHub
parent 76b917f432
commit b1b32b2064
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      validator.go
  2. 15
      validator_test.go

@ -227,7 +227,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
} }
} }
if !ct.hasTag { if ct == nil || !ct.hasTag {
return return
} }

@ -3285,6 +3285,21 @@ func TestMapDiveValidation(t *testing.T) {
s := fmt.Sprint(errs.Error()) s := fmt.Sprint(errs.Error())
NotEqual(t, s, "") NotEqual(t, s, "")
type TestMapInterface struct {
Errs map[int]interface{} `validate:"dive"`
}
mit := map[int]interface{}{0: Inner{"ok"}, 1: Inner{""}, 3: nil, 5: "string", 6: 33}
msi := &TestMapInterface{
Errs: mit,
}
errs = validate.Struct(msi)
NotEqual(t, errs, nil)
Equal(t, len(errs.(ValidationErrors)), 1)
AssertError(t, errs, "TestMapInterface.Errs[1].Name", "TestMapInterface.Errs[1].Name", "Name", "Name", "required")
type TestMapTimeStruct struct { type TestMapTimeStruct struct {
Errs map[int]*time.Time `validate:"gt=0,dive,required"` Errs map[int]*time.Time `validate:"gt=0,dive,required"`
} }

Loading…
Cancel
Save