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

Fixed boolean validation to handle bool kind #988

Merged
merged 1 commit into from Sep 16, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions baked_in.go
Expand Up @@ -1484,10 +1484,15 @@ func isAlphaUnicode(fl FieldLevel) bool {
return alphaUnicodeRegex.MatchString(fl.Field().String())
}

// isBoolean is the validation function for validating if the current field's value can be safely converted to a boolean.
// isBoolean is the validation function for validating if the current field's value is a valid boolean value or can be safely converted to a boolean value.
func isBoolean(fl FieldLevel) bool {
_, err := strconv.ParseBool(fl.Field().String())
return err == nil
switch fl.Field().Kind() {
case reflect.Bool:
return true
default:
_, err := strconv.ParseBool(fl.Field().String())
return err == nil
}
}

// isDefault is the opposite of required aka hasValue
Expand Down
81 changes: 59 additions & 22 deletions validator_test.go
Expand Up @@ -8302,6 +8302,43 @@ func TestNumeric(t *testing.T) {
errs = validate.Var(i, "numeric")
Equal(t, errs, nil)
}
func TestBoolean(t *testing.T) {
validate := New()

b := true
errs := validate.Var(b, "boolean")
Equal(t, errs, nil)

b = false
errs = validate.Var(b, "boolean")
Equal(t, errs, nil)

s := "true"
errs = validate.Var(s, "boolean")
Equal(t, errs, nil)

s = "false"
errs = validate.Var(s, "boolean")
Equal(t, errs, nil)

s = "0"
errs = validate.Var(s, "boolean")
Equal(t, errs, nil)

s = "1"
errs = validate.Var(s, "boolean")
Equal(t, errs, nil)

s = "xyz"
errs = validate.Var(s, "boolean")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "boolean")

s = "1."
errs = validate.Var(s, "boolean")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "boolean")
}

func TestAlphaNumeric(t *testing.T) {
validate := New()
Expand Down Expand Up @@ -12264,25 +12301,25 @@ func TestCreditCardFormatValidation(t *testing.T) {
}

func TestMultiOrOperatorGroup(t *testing.T) {
tests := []struct {
Value int `validate:"eq=1|gte=5,eq=1|lt=7"`
expected bool
}{
{1, true}, {2, false}, {5, true}, {6, true}, {8, false},
}

validate := New()

for i, test := range tests {
errs := validate.Struct(test)
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d multi_group_of_OR_operators failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d multi_group_of_OR_operators should have errs", i)
}
}
}
}
tests := []struct {
Value int `validate:"eq=1|gte=5,eq=1|lt=7"`
expected bool
}{
{1, true}, {2, false}, {5, true}, {6, true}, {8, false},
}

validate := New()

for i, test := range tests {
errs := validate.Struct(test)
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d multi_group_of_OR_operators failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d multi_group_of_OR_operators should have errs", i)
}
}
}
}