Skip to content

Commit 0c1335d

Browse files
authored
Added alphanumspace string validator (#1484)
## 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 I’ve been using this package in several projects at our company. Yesterday, while searching for a validation tag that supports alphanumeric characters and spaces for Go structs, I was unable to find an existing one. To address this, I created a regex pattern that allows alphabets, numbers, and spaces. This pull request adds that regex-based validation tag to the package, extending its functionality for common text fields requiring alphanumeric and space support. Summary of Changes Added a new regex validation tag supporting alphabets, numbers, and spaces Included documentation and tests for the new tag
1 parent c6a4fb6 commit 0c1335d

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ validate := validator.New(validator.WithRequiredStructEnabled())
137137
| alpha | Alpha Only |
138138
| alphaspace | Alpha Space |
139139
| alphanum | Alphanumeric |
140+
| alphanumspace | Alphanumeric Space |
140141
| alphanumunicode | Alphanumeric Unicode |
141142
| alphaunicode | Alpha Unicode |
142143
| ascii | ASCII |

baked_in.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ var (
120120
"alpha": isAlpha,
121121
"alphaspace": isAlphaSpace,
122122
"alphanum": isAlphanum,
123+
"alphanumspace": isAlphaNumericSpace,
123124
"alphaunicode": isAlphaUnicode,
124125
"alphanumunicode": isAlphanumUnicode,
125126
"boolean": isBoolean,
@@ -1782,6 +1783,11 @@ func isAlphaSpace(fl FieldLevel) bool {
17821783
return alphaSpaceRegex().MatchString(fl.Field().String())
17831784
}
17841785

1786+
// isAlphaNumericSpace is the validation function for validating if the current field's value is a valid alphanumeric value with spaces.
1787+
func isAlphaNumericSpace(fl FieldLevel) bool {
1788+
return alphanNumericSpaceRegex().MatchString(fl.Field().String())
1789+
}
1790+
17851791
// isAlphaUnicode is the validation function for validating if the current field's value is a valid alpha unicode value.
17861792
func isAlphaUnicode(fl FieldLevel) bool {
17871793
return alphaUnicodeRegex().MatchString(fl.Field().String())

doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,12 @@ This validates that a string value contains ASCII alphanumeric characters only
789789
790790
Usage: alphanum
791791
792+
# Alphanumeric Space
793+
794+
This validates that a string value contains ASCII alphanumeric characters and spaces only
795+
796+
Usage: alphanumspace
797+
792798
# Alpha Unicode
793799
794800
This validates that a string value contains unicode alpha characters only

regexes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const (
99
alphaRegexString = "^[a-zA-Z]+$"
1010
alphaSpaceRegexString = "^[a-zA-Z ]+$"
1111
alphaNumericRegexString = "^[a-zA-Z0-9]+$"
12+
alphaNumericSpaceRegexString = "^[a-zA-Z0-9 ]+$"
1213
alphaUnicodeRegexString = "^[\\p{L}]+$"
1314
alphaUnicodeNumericRegexString = "^[\\p{L}\\p{N}]+$"
1415
numericRegexString = "^[-+]?[0-9]+(?:\\.[0-9]+)?$"
@@ -96,6 +97,7 @@ func lazyRegexCompile(str string) func() *regexp.Regexp {
9697
var (
9798
alphaRegex = lazyRegexCompile(alphaRegexString)
9899
alphaSpaceRegex = lazyRegexCompile(alphaSpaceRegexString)
100+
alphanNumericSpaceRegex = lazyRegexCompile(alphaNumericSpaceRegexString)
99101
alphaNumericRegex = lazyRegexCompile(alphaNumericRegexString)
100102
alphaUnicodeRegex = lazyRegexCompile(alphaUnicodeRegexString)
101103
alphaUnicodeNumericRegex = lazyRegexCompile(alphaUnicodeNumericRegexString)

validator_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9100,6 +9100,73 @@ func TestAlphaSpace(t *testing.T) {
91009100
AssertError(t, errs, "", "", "", "", "alphaspace")
91019101
}
91029102

9103+
func TestAlphaNumericSpace(t *testing.T) {
9104+
validate := New()
9105+
9106+
s := "abcd 123"
9107+
errs := validate.Var(s, "alphanumspace")
9108+
Equal(t, errs, nil)
9109+
9110+
s = " "
9111+
errs = validate.Var(s, "alphanumspace")
9112+
Equal(t, errs, nil)
9113+
9114+
s = "abc123"
9115+
errs = validate.Var(s, "alphanumspace")
9116+
Equal(t, errs, nil)
9117+
9118+
s = "123"
9119+
errs = validate.Var(s, "alphanumspace")
9120+
Equal(t, errs, nil)
9121+
9122+
s = "abc"
9123+
errs = validate.Var(s, "alphanumspace")
9124+
Equal(t, errs, nil)
9125+
9126+
s = "áçć 123"
9127+
errs = validate.Var(s, "alphanumspace")
9128+
NotEqual(t, errs, nil)
9129+
AssertError(t, errs, "", "", "", "", "alphanumspace")
9130+
9131+
s = "日本 123"
9132+
errs = validate.Var(s, "alphanumspace")
9133+
NotEqual(t, errs, nil)
9134+
AssertError(t, errs, "", "", "", "", "alphanumspace")
9135+
9136+
s = "abc!"
9137+
errs = validate.Var(s, "alphanumspace")
9138+
NotEqual(t, errs, nil)
9139+
AssertError(t, errs, "", "", "", "", "alphanumspace")
9140+
9141+
s = "abc\t123"
9142+
errs = validate.Var(s, "alphanumspace")
9143+
NotEqual(t, errs, nil)
9144+
AssertError(t, errs, "", "", "", "", "alphanumspace")
9145+
9146+
s = "abc\n123"
9147+
errs = validate.Var(s, "alphanumspace")
9148+
NotEqual(t, errs, nil)
9149+
AssertError(t, errs, "", "", "", "", "alphanumspace")
9150+
9151+
s = "abc-123"
9152+
errs = validate.Var(s, "alphanumspace")
9153+
NotEqual(t, errs, nil)
9154+
AssertError(t, errs, "", "", "", "", "alphanumspace")
9155+
9156+
s = "abc🙂123"
9157+
errs = validate.Var(s, "alphanumspace")
9158+
NotEqual(t, errs, nil)
9159+
AssertError(t, errs, "", "", "", "", "alphanumspace")
9160+
9161+
errs = validate.Var(1, "alphanumspace")
9162+
NotEqual(t, errs, nil)
9163+
AssertError(t, errs, "", "", "", "", "alphanumspace")
9164+
9165+
errs = validate.Var(1.23, "alphanumspace")
9166+
NotEqual(t, errs, nil)
9167+
AssertError(t, errs, "", "", "", "", "alphanumspace")
9168+
}
9169+
91039170
func TestStructStringValidation(t *testing.T) {
91049171
validate := New()
91059172

0 commit comments

Comments
 (0)