Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#1310): fix excluded_if for pointers #1313

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -1828,7 +1828,14 @@ func requireCheckFieldValue(
return int64(field.Len()) == asInt(value)

case reflect.Bool:
return field.Bool() == asBool(value)
return field.Bool() == (value == "true")

case reflect.Ptr:
if field.IsNil() {
return value == "nil"
}
// Handle non-nil pointers
return requireCheckFieldValue(fl, param, value, defaultNotFoundValue)
}

// default reflect.String:
Expand Down
19 changes: 19 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12002,6 +12002,25 @@ func TestExcludedIf(t *testing.T) {
errs = validate.Struct(test10)
Equal(t, errs, nil)

test11 := struct {
Field1 bool
Field2 *string `validate:"excluded_if=Field1 false"`
}{
Field1: false,
Field2: nil,
}
errs = validate.Struct(test11)
Equal(t, errs, nil)

test12 := struct {
Field1 bool
Field2 *string `validate:"excluded_if=Field1 !Field1"`
}{
Field1: true,
Field2: nil,
}
errs = validate.Struct(test12)
Equal(t, errs, nil)
// Checks number of params in struct tag is correct
defer func() {
if r := recover(); r == nil {
Expand Down