Skip to content

Commit

Permalink
Fix map diving validation (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
echovl authored Jul 8, 2021
1 parent 76b917f commit b1b32b2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
}
}

if !ct.hasTag {
if ct == nil || !ct.hasTag {
return
}

Expand Down
15 changes: 15 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3285,6 +3285,21 @@ func TestMapDiveValidation(t *testing.T) {
s := fmt.Sprint(errs.Error())
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 {
Errs map[int]*time.Time `validate:"gt=0,dive,required"`
}
Expand Down

0 comments on commit b1b32b2

Please sign in to comment.