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

Add boolean baked-in validator to check a string value is a valid boolean representation #804

Merged
merged 2 commits into from Aug 8, 2021
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
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -126,6 +126,7 @@ Baked-in Validations
| alphanumunicode | Alphanumeric Unicode |
| alphaunicode | Alpha Unicode |
| ascii | ASCII |
| boolean | Boolean |
| contains | Contains |
| containsany | Contains Any |
| containsrune | Contains Rune |
Expand Down
7 changes: 7 additions & 0 deletions baked_in.go
Expand Up @@ -107,6 +107,7 @@ var (
"alphanum": isAlphanum,
"alphaunicode": isAlphaUnicode,
"alphanumunicode": isAlphanumUnicode,
"boolean": isBoolean,
"numeric": isNumeric,
"number": isNumber,
"hexadecimal": isHexadecimal,
Expand Down Expand Up @@ -1438,6 +1439,12 @@ 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.
func isBoolean(fl FieldLevel) bool {
_, err := strconv.ParseBool(fl.Field().String())
return err == nil
}

// isDefault is the opposite of required aka hasValue
func isDefault(fl FieldLevel) bool {
return !hasValue(fl)
Expand Down
6 changes: 6 additions & 0 deletions doc.go
Expand Up @@ -726,6 +726,12 @@ This validates that a string value contains unicode alphanumeric characters only

Usage: alphanumunicode

Boolean

This validates that a string value can successfully be parsed into a boolean with strconv.ParseBool

Usage: boolean

Number

This validates that a string value contains number values only.
Expand Down
6 changes: 5 additions & 1 deletion validator_test.go
Expand Up @@ -71,6 +71,7 @@ type TestString struct {
Gt string `validate:"gt=10"`
Gte string `validate:"gte=10"`
OmitEmpty string `validate:"omitempty,min=1,max=10"`
Boolean string `validate:"boolean"`
Sub *SubTest
SubIgnore *SubTest `validate:"-"`
Anonymous struct {
Expand Down Expand Up @@ -7943,6 +7944,7 @@ func TestStructStringValidation(t *testing.T) {
Lte: "0123456789",
Gt: "01234567890",
Gte: "0123456789",
Boolean: "true",
OmitEmpty: "",
Sub: &SubTest{
Test: "1",
Expand Down Expand Up @@ -7974,6 +7976,7 @@ func TestStructStringValidation(t *testing.T) {
Gt: "1",
Gte: "1",
OmitEmpty: "12345678901",
Boolean: "nope",
Sub: &SubTest{
Test: "",
},
Expand All @@ -7991,7 +7994,7 @@ func TestStructStringValidation(t *testing.T) {

// Assert Top Level
NotEqual(t, errs, nil)
Equal(t, len(errs.(ValidationErrors)), 13)
Equal(t, len(errs.(ValidationErrors)), 14)

// Assert Fields
AssertError(t, errs, "TestString.Required", "TestString.Required", "Required", "Required", "required")
Expand All @@ -8004,6 +8007,7 @@ func TestStructStringValidation(t *testing.T) {
AssertError(t, errs, "TestString.Gt", "TestString.Gt", "Gt", "Gt", "gt")
AssertError(t, errs, "TestString.Gte", "TestString.Gte", "Gte", "Gte", "gte")
AssertError(t, errs, "TestString.OmitEmpty", "TestString.OmitEmpty", "OmitEmpty", "OmitEmpty", "max")
AssertError(t, errs, "TestString.Boolean", "TestString.Boolean", "Boolean", "Boolean", "boolean")

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