Skip to content

Commit

Permalink
fix(#1310): fix excluded_if for pointers (#1313)
Browse files Browse the repository at this point in the history
## Fixes Or Enhances
- fixes #1310
- add check for pointers in case of excluded_if
  • Loading branch information
ganeshdipdumbare committed Sep 9, 2024
1 parent a947377 commit 4a073cc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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

0 comments on commit 4a073cc

Please sign in to comment.