Repository: go-playground/validator
Pull request #988 (2022-08-18 08:16:20)

Fixed boolean validation to handle bool kind
## Fixes Or Enhances


**Make sure that you've checked the boxes below before you submit PR:**
- [x] Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

coveralls: 
[![Coverage Status](https://coveralls.io/builds/51756528/badge)](https://coveralls.io/builds/51756528)

Coverage increased (+0.005%) to 74.182% when pulling **a193dc2270aa429563c31a39b27a762a2c931b0c on V-R-Dighe:develop** into **9e2ea4038020b5c7e3802a21cfa4e3afcfdcd276 on go-playground:master**.

V-R-Dighe: @deankarn can you please review this PR and let me know if you have any suggested changes?
V-R-Dighe: Hello Team,
cc: @deankarn 

Please review and let me know if anything more is needed from my end.

Thanks,
Vaibhav Dighe
deankarn: Thanks for the reminder @V-R-Dighe sorry i just have very little time to maintain this project ATM.

will try to slip it in tomorrow.

Patch:
diff --git a/baked_in.go b/baked_in.go
index f2f0939..c9b1db4 100644
--- a/baked_in.go
+++ b/baked_in.go
@@ -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


Test patch:
diff --git a/validator_test.go b/validator_test.go
index 1ebdea5..7e314d6 100644
--- a/validator_test.go
+++ b/validator_test.go
@@ -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()
@@ -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)
+			}
+		}
+	}
+}