Skip to content

Commit 5187f87

Browse files
feat: add base32 validator tag (#1253)
1 parent 21b0b9f commit 5187f87

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

baked_in.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ var (
133133
"urn_rfc2141": isUrnRFC2141, // RFC 2141
134134
"file": isFile,
135135
"filepath": isFilePath,
136+
"base32": isBase32,
136137
"base64": isBase64,
137138
"base64url": isBase64URL,
138139
"base64rawurl": isBase64RawURL,
@@ -1399,6 +1400,11 @@ func isPostcodeByIso3166Alpha2Field(fl FieldLevel) bool {
13991400
return reg.MatchString(field.String())
14001401
}
14011402

1403+
// isBase32 is the validation function for validating if the current field's value is a valid base 32.
1404+
func isBase32(fl FieldLevel) bool {
1405+
return base32Regex.MatchString(fl.Field().String())
1406+
}
1407+
14021408
// isBase64 is the validation function for validating if the current field's value is a valid base 64.
14031409
func isBase64(fl FieldLevel) bool {
14041410
return base64Regex.MatchString(fl.Field().String())

doc.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,15 @@ according to the RFC 2141 spec.
916916
917917
Usage: urn_rfc2141
918918
919+
# Base32 String
920+
921+
This validates that a string value contains a valid bas324 value.
922+
Although an empty string is valid base32 this will report an empty string
923+
as an error, if you wish to accept an empty string as valid you can use
924+
this with the omitempty tag.
925+
926+
Usage: base32
927+
919928
# Base64 String
920929
921930
This validates that a string value contains a valid base64 value.

regexes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const (
1717
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*\\)$"
1818
emailRegexString = "^(?:(?:(?:(?:[a-zA-Z]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}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
1919
e164RegexString = "^\\+[1-9]?[0-9]{7,14}$"
20+
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})$"
2021
base64RegexString = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$"
2122
base64URLRegexString = "^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2}==|[A-Za-z0-9-_]{3}=|[A-Za-z0-9-_]{4})$"
2223
base64RawURLRegexString = "^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2,4})$"
@@ -89,6 +90,7 @@ var (
8990
hslaRegex = regexp.MustCompile(hslaRegexString)
9091
e164Regex = regexp.MustCompile(e164RegexString)
9192
emailRegex = regexp.MustCompile(emailRegexString)
93+
base32Regex = regexp.MustCompile(base32RegexString)
9294
base64Regex = regexp.MustCompile(base64RegexString)
9395
base64URLRegex = regexp.MustCompile(base64URLRegexString)
9496
base64RawURLRegex = regexp.MustCompile(base64RawURLRegexString)

validator_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5680,6 +5680,40 @@ func TestOneOfValidation(t *testing.T) {
56805680
}, "Bad field type float64")
56815681
}
56825682

5683+
func TestBase32Validation(t *testing.T) {
5684+
validate := New()
5685+
5686+
s := "ABCD2345"
5687+
errs := validate.Var(s, "base32")
5688+
Equal(t, errs, nil)
5689+
5690+
s = "AB======"
5691+
errs = validate.Var(s, "base32")
5692+
Equal(t, errs, nil)
5693+
5694+
s = "ABCD2==="
5695+
errs = validate.Var(s, "base32")
5696+
Equal(t, errs, nil)
5697+
5698+
s = "ABCD===="
5699+
errs = validate.Var(s, "base32")
5700+
Equal(t, errs, nil)
5701+
5702+
s = "ABCD234="
5703+
errs = validate.Var(s, "base32")
5704+
Equal(t, errs, nil)
5705+
5706+
s = ""
5707+
errs = validate.Var(s, "base32")
5708+
NotEqual(t, errs, nil)
5709+
AssertError(t, errs, "", "", "", "", "base32")
5710+
5711+
s = "ABCabc1890== foo bar"
5712+
errs = validate.Var(s, "base32")
5713+
NotEqual(t, errs, nil)
5714+
AssertError(t, errs, "", "", "", "", "base32")
5715+
}
5716+
56835717
func TestBase64Validation(t *testing.T) {
56845718
validate := New()
56855719

0 commit comments

Comments
 (0)