Skip to content

Commit b1b32b2

Browse files
authored
Fix map diving validation (#793)
1 parent 76b917f commit b1b32b2

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
227227
}
228228
}
229229

230-
if !ct.hasTag {
230+
if ct == nil || !ct.hasTag {
231231
return
232232
}
233233

validator_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3285,6 +3285,21 @@ func TestMapDiveValidation(t *testing.T) {
32853285
s := fmt.Sprint(errs.Error())
32863286
NotEqual(t, s, "")
32873287

3288+
type TestMapInterface struct {
3289+
Errs map[int]interface{} `validate:"dive"`
3290+
}
3291+
3292+
mit := map[int]interface{}{0: Inner{"ok"}, 1: Inner{""}, 3: nil, 5: "string", 6: 33}
3293+
3294+
msi := &TestMapInterface{
3295+
Errs: mit,
3296+
}
3297+
3298+
errs = validate.Struct(msi)
3299+
NotEqual(t, errs, nil)
3300+
Equal(t, len(errs.(ValidationErrors)), 1)
3301+
AssertError(t, errs, "TestMapInterface.Errs[1].Name", "TestMapInterface.Errs[1].Name", "Name", "Name", "required")
3302+
32883303
type TestMapTimeStruct struct {
32893304
Errs map[int]*time.Time `validate:"gt=0,dive,required"`
32903305
}

0 commit comments

Comments
 (0)