Skip to content

Commit 9593a0f

Browse files
author
Dean Karn
committed
add test for new nil validation
1 parent 06f9224 commit 9593a0f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

validator_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8839,3 +8839,28 @@ func TestLookup(t *testing.T) {
88398839
}
88408840
Equal(t, New().Struct(lookup), nil)
88418841
}
8842+
8843+
func TestAbilityToValidateNils(t *testing.T) {
8844+
8845+
type TestStruct struct {
8846+
Test *string `validate:"nil"`
8847+
}
8848+
8849+
ts := TestStruct{}
8850+
val := New()
8851+
fn := func(fl FieldLevel) bool {
8852+
return fl.Field().Kind() == reflect.Ptr && fl.Field().IsNil()
8853+
}
8854+
8855+
err := val.RegisterValidation("nil", fn, true)
8856+
Equal(t, err, nil)
8857+
8858+
errs := val.Struct(ts)
8859+
Equal(t, errs, nil)
8860+
8861+
str := "string"
8862+
ts.Test = &str
8863+
8864+
errs = val.Struct(ts)
8865+
NotEqual(t, errs, nil)
8866+
}

0 commit comments

Comments
 (0)