Skip to content

Commit a67baa7

Browse files
authored
feature/boolean (#804)
1 parent 14221d0 commit a67baa7

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Baked-in Validations
126126
| alphanumunicode | Alphanumeric Unicode |
127127
| alphaunicode | Alpha Unicode |
128128
| ascii | ASCII |
129+
| boolean | Boolean |
129130
| contains | Contains |
130131
| containsany | Contains Any |
131132
| containsrune | Contains Rune |

baked_in.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ var (
107107
"alphanum": isAlphanum,
108108
"alphaunicode": isAlphaUnicode,
109109
"alphanumunicode": isAlphanumUnicode,
110+
"boolean": isBoolean,
110111
"numeric": isNumeric,
111112
"number": isNumber,
112113
"hexadecimal": isHexadecimal,
@@ -1438,6 +1439,12 @@ func isAlphaUnicode(fl FieldLevel) bool {
14381439
return alphaUnicodeRegex.MatchString(fl.Field().String())
14391440
}
14401441

1442+
// isBoolean is the validation function for validating if the current field's value can be safely converted to a boolean.
1443+
func isBoolean(fl FieldLevel) bool {
1444+
_, err := strconv.ParseBool(fl.Field().String())
1445+
return err == nil
1446+
}
1447+
14411448
// isDefault is the opposite of required aka hasValue
14421449
func isDefault(fl FieldLevel) bool {
14431450
return !hasValue(fl)

doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,12 @@ This validates that a string value contains unicode alphanumeric characters only
726726
727727
Usage: alphanumunicode
728728
729+
Boolean
730+
731+
This validates that a string value can successfully be parsed into a boolean with strconv.ParseBool
732+
733+
Usage: boolean
734+
729735
Number
730736
731737
This validates that a string value contains number values only.

validator_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ type TestString struct {
7171
Gt string `validate:"gt=10"`
7272
Gte string `validate:"gte=10"`
7373
OmitEmpty string `validate:"omitempty,min=1,max=10"`
74+
Boolean string `validate:"boolean"`
7475
Sub *SubTest
7576
SubIgnore *SubTest `validate:"-"`
7677
Anonymous struct {
@@ -7943,6 +7944,7 @@ func TestStructStringValidation(t *testing.T) {
79437944
Lte: "0123456789",
79447945
Gt: "01234567890",
79457946
Gte: "0123456789",
7947+
Boolean: "true",
79467948
OmitEmpty: "",
79477949
Sub: &SubTest{
79487950
Test: "1",
@@ -7974,6 +7976,7 @@ func TestStructStringValidation(t *testing.T) {
79747976
Gt: "1",
79757977
Gte: "1",
79767978
OmitEmpty: "12345678901",
7979+
Boolean: "nope",
79777980
Sub: &SubTest{
79787981
Test: "",
79797982
},
@@ -7991,7 +7994,7 @@ func TestStructStringValidation(t *testing.T) {
79917994

79927995
// Assert Top Level
79937996
NotEqual(t, errs, nil)
7994-
Equal(t, len(errs.(ValidationErrors)), 13)
7997+
Equal(t, len(errs.(ValidationErrors)), 14)
79957998

79967999
// Assert Fields
79978000
AssertError(t, errs, "TestString.Required", "TestString.Required", "Required", "Required", "required")
@@ -8004,6 +8007,7 @@ func TestStructStringValidation(t *testing.T) {
80048007
AssertError(t, errs, "TestString.Gt", "TestString.Gt", "Gt", "Gt", "gt")
80058008
AssertError(t, errs, "TestString.Gte", "TestString.Gte", "Gte", "Gte", "gte")
80068009
AssertError(t, errs, "TestString.OmitEmpty", "TestString.OmitEmpty", "OmitEmpty", "OmitEmpty", "max")
8010+
AssertError(t, errs, "TestString.Boolean", "TestString.Boolean", "Boolean", "Boolean", "boolean")
80078011

80088012
// Nested Struct Field Errs
80098013
AssertError(t, errs, "TestString.Anonymous.A", "TestString.Anonymous.A", "A", "A", "required")

0 commit comments

Comments
 (0)