Skip to content

Commit

Permalink
feat: add base32 validator tag (#1253)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesatkin-myndup committed Apr 30, 2024
1 parent 21b0b9f commit 5187f87
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
6 changes: 6 additions & 0 deletions baked_in.go
Expand Up @@ -133,6 +133,7 @@ var (
"urn_rfc2141": isUrnRFC2141, // RFC 2141
"file": isFile,
"filepath": isFilePath,
"base32": isBase32,
"base64": isBase64,
"base64url": isBase64URL,
"base64rawurl": isBase64RawURL,
Expand Down Expand Up @@ -1399,6 +1400,11 @@ func isPostcodeByIso3166Alpha2Field(fl FieldLevel) bool {
return reg.MatchString(field.String())
}

// isBase32 is the validation function for validating if the current field's value is a valid base 32.
func isBase32(fl FieldLevel) bool {
return base32Regex.MatchString(fl.Field().String())
}

// isBase64 is the validation function for validating if the current field's value is a valid base 64.
func isBase64(fl FieldLevel) bool {
return base64Regex.MatchString(fl.Field().String())
Expand Down
9 changes: 9 additions & 0 deletions doc.go
Expand Up @@ -916,6 +916,15 @@ according to the RFC 2141 spec.
Usage: urn_rfc2141
# Base32 String
This validates that a string value contains a valid bas324 value.
Although an empty string is valid base32 this will report an empty string
as an error, if you wish to accept an empty string as valid you can use
this with the omitempty tag.
Usage: base32
# Base64 String
This validates that a string value contains a valid base64 value.
Expand Down
2 changes: 2 additions & 0 deletions regexes.go
Expand Up @@ -17,6 +17,7 @@ const (
hslaRegexString = "^hsla\\(\\s*(?:0|[1-9]\\d?|[12]\\d\\d|3[0-5]\\d|360)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*,\\s*(?:(?:0.[1-9]*)|[01])\\s*\\)$"
emailRegexString = "^(?:(?:(?:(?:[a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF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[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
e164RegexString = "^\\+[1-9]?[0-9]{7,14}$"
base32RegexString = "^(?:[A-Z2-7]{8})*(?:[A-Z2-7]{2}={6}|[A-Z2-7]{4}={4}|[A-Z2-7]{5}={3}|[A-Z2-7]{7}=|[A-Z2-7]{8})$"
base64RegexString = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$"
base64URLRegexString = "^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2}==|[A-Za-z0-9-_]{3}=|[A-Za-z0-9-_]{4})$"
base64RawURLRegexString = "^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2,4})$"
Expand Down Expand Up @@ -89,6 +90,7 @@ var (
hslaRegex = regexp.MustCompile(hslaRegexString)
e164Regex = regexp.MustCompile(e164RegexString)
emailRegex = regexp.MustCompile(emailRegexString)
base32Regex = regexp.MustCompile(base32RegexString)
base64Regex = regexp.MustCompile(base64RegexString)
base64URLRegex = regexp.MustCompile(base64URLRegexString)
base64RawURLRegex = regexp.MustCompile(base64RawURLRegexString)
Expand Down
34 changes: 34 additions & 0 deletions validator_test.go
Expand Up @@ -5680,6 +5680,40 @@ func TestOneOfValidation(t *testing.T) {
}, "Bad field type float64")
}

func TestBase32Validation(t *testing.T) {
validate := New()

s := "ABCD2345"
errs := validate.Var(s, "base32")
Equal(t, errs, nil)

s = "AB======"
errs = validate.Var(s, "base32")
Equal(t, errs, nil)

s = "ABCD2==="
errs = validate.Var(s, "base32")
Equal(t, errs, nil)

s = "ABCD===="
errs = validate.Var(s, "base32")
Equal(t, errs, nil)

s = "ABCD234="
errs = validate.Var(s, "base32")
Equal(t, errs, nil)

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

s = "ABCabc1890== foo bar"
errs = validate.Var(s, "base32")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "base32")
}

func TestBase64Validation(t *testing.T) {
validate := New()

Expand Down

0 comments on commit 5187f87

Please sign in to comment.