Skip to content

Commit 1e8c614

Browse files
authored
Fixed boolean validation to handle bool kind (#988)
1 parent 9e2ea40 commit 1e8c614

File tree

2 files changed

+67
-25
lines changed

2 files changed

+67
-25
lines changed

baked_in.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,10 +1484,15 @@ func isAlphaUnicode(fl FieldLevel) bool {
14841484
return alphaUnicodeRegex.MatchString(fl.Field().String())
14851485
}
14861486

1487-
// isBoolean is the validation function for validating if the current field's value can be safely converted to a boolean.
1487+
// 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.
14881488
func isBoolean(fl FieldLevel) bool {
1489-
_, err := strconv.ParseBool(fl.Field().String())
1490-
return err == nil
1489+
switch fl.Field().Kind() {
1490+
case reflect.Bool:
1491+
return true
1492+
default:
1493+
_, err := strconv.ParseBool(fl.Field().String())
1494+
return err == nil
1495+
}
14911496
}
14921497

14931498
// isDefault is the opposite of required aka hasValue

validator_test.go

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8302,6 +8302,43 @@ func TestNumeric(t *testing.T) {
83028302
errs = validate.Var(i, "numeric")
83038303
Equal(t, errs, nil)
83048304
}
8305+
func TestBoolean(t *testing.T) {
8306+
validate := New()
8307+
8308+
b := true
8309+
errs := validate.Var(b, "boolean")
8310+
Equal(t, errs, nil)
8311+
8312+
b = false
8313+
errs = validate.Var(b, "boolean")
8314+
Equal(t, errs, nil)
8315+
8316+
s := "true"
8317+
errs = validate.Var(s, "boolean")
8318+
Equal(t, errs, nil)
8319+
8320+
s = "false"
8321+
errs = validate.Var(s, "boolean")
8322+
Equal(t, errs, nil)
8323+
8324+
s = "0"
8325+
errs = validate.Var(s, "boolean")
8326+
Equal(t, errs, nil)
8327+
8328+
s = "1"
8329+
errs = validate.Var(s, "boolean")
8330+
Equal(t, errs, nil)
8331+
8332+
s = "xyz"
8333+
errs = validate.Var(s, "boolean")
8334+
NotEqual(t, errs, nil)
8335+
AssertError(t, errs, "", "", "", "", "boolean")
8336+
8337+
s = "1."
8338+
errs = validate.Var(s, "boolean")
8339+
NotEqual(t, errs, nil)
8340+
AssertError(t, errs, "", "", "", "", "boolean")
8341+
}
83058342

83068343
func TestAlphaNumeric(t *testing.T) {
83078344
validate := New()
@@ -12264,25 +12301,25 @@ func TestCreditCardFormatValidation(t *testing.T) {
1226412301
}
1226512302

1226612303
func TestMultiOrOperatorGroup(t *testing.T) {
12267-
tests := []struct {
12268-
Value int `validate:"eq=1|gte=5,eq=1|lt=7"`
12269-
expected bool
12270-
}{
12271-
{1, true}, {2, false}, {5, true}, {6, true}, {8, false},
12272-
}
12273-
12274-
validate := New()
12275-
12276-
for i, test := range tests {
12277-
errs := validate.Struct(test)
12278-
if test.expected {
12279-
if !IsEqual(errs, nil) {
12280-
t.Fatalf("Index: %d multi_group_of_OR_operators failed Error: %s", i, errs)
12281-
}
12282-
} else {
12283-
if IsEqual(errs, nil) {
12284-
t.Fatalf("Index: %d multi_group_of_OR_operators should have errs", i)
12285-
}
12286-
}
12287-
}
12288-
}
12304+
tests := []struct {
12305+
Value int `validate:"eq=1|gte=5,eq=1|lt=7"`
12306+
expected bool
12307+
}{
12308+
{1, true}, {2, false}, {5, true}, {6, true}, {8, false},
12309+
}
12310+
12311+
validate := New()
12312+
12313+
for i, test := range tests {
12314+
errs := validate.Struct(test)
12315+
if test.expected {
12316+
if !IsEqual(errs, nil) {
12317+
t.Fatalf("Index: %d multi_group_of_OR_operators failed Error: %s", i, errs)
12318+
}
12319+
} else {
12320+
if IsEqual(errs, nil) {
12321+
t.Fatalf("Index: %d multi_group_of_OR_operators should have errs", i)
12322+
}
12323+
}
12324+
}
12325+
}

0 commit comments

Comments
 (0)