Skip to content

Commit bdc3a7d

Browse files
authored
Add alphaspace validator (#1343)
1 parent 63594a0 commit bdc3a7d

File tree

5 files changed

+40
-0
lines changed

5 files changed

+40
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ validate := validator.New(validator.WithRequiredStructEnabled())
135135
| Tag | Description |
136136
| - | - |
137137
| alpha | Alpha Only |
138+
| alphaspace | Alpha Space |
138139
| alphanum | Alphanumeric |
139140
| alphanumunicode | Alphanumeric Unicode |
140141
| alphaunicode | Alpha Unicode |

baked_in.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ var (
118118
"fieldcontains": fieldContains,
119119
"fieldexcludes": fieldExcludes,
120120
"alpha": isAlpha,
121+
"alphaspace": isAlphaSpace,
121122
"alphanum": isAlphanum,
122123
"alphaunicode": isAlphaUnicode,
123124
"alphanumunicode": isAlphanumUnicode,
@@ -1767,6 +1768,11 @@ func isAlphanumUnicode(fl FieldLevel) bool {
17671768
return alphaUnicodeNumericRegex().MatchString(fl.Field().String())
17681769
}
17691770

1771+
// isAlphaSpace is the validation function for validating if the current field's value is a valid alpha value with spaces.
1772+
func isAlphaSpace(fl FieldLevel) bool {
1773+
return alphaSpaceRegex().MatchString(fl.Field().String())
1774+
}
1775+
17701776
// isAlphaUnicode is the validation function for validating if the current field's value is a valid alpha unicode value.
17711777
func isAlphaUnicode(fl FieldLevel) bool {
17721778
return alphaUnicodeRegex().MatchString(fl.Field().String())

doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,12 @@ This validates that a string value contains ASCII alpha characters only
777777
778778
Usage: alpha
779779
780+
# Alpha Space
781+
782+
This validates that a string value contains ASCII alpha characters and spaces only
783+
784+
Usage: alphaspace
785+
780786
# Alphanumeric
781787
782788
This validates that a string value contains ASCII alphanumeric characters only

regexes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
const (
99
alphaRegexString = "^[a-zA-Z]+$"
10+
alphaSpaceRegexString = "^[a-zA-Z ]+$"
1011
alphaNumericRegexString = "^[a-zA-Z0-9]+$"
1112
alphaUnicodeRegexString = "^[\\p{L}]+$"
1213
alphaUnicodeNumericRegexString = "^[\\p{L}\\p{N}]+$"
@@ -93,6 +94,7 @@ func lazyRegexCompile(str string) func() *regexp.Regexp {
9394

9495
var (
9596
alphaRegex = lazyRegexCompile(alphaRegexString)
97+
alphaSpaceRegex = lazyRegexCompile(alphaSpaceRegexString)
9698
alphaNumericRegex = lazyRegexCompile(alphaNumericRegexString)
9799
alphaUnicodeRegex = lazyRegexCompile(alphaUnicodeRegexString)
98100
alphaUnicodeNumericRegex = lazyRegexCompile(alphaUnicodeNumericRegexString)

validator_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9044,6 +9044,31 @@ func TestAlpha(t *testing.T) {
90449044
AssertError(t, errs, "", "", "", "", "alpha")
90459045
}
90469046

9047+
func TestAlphaSpace(t *testing.T) {
9048+
validate := New()
9049+
9050+
s := "abcd"
9051+
errs := validate.Var(s, "alphaspace")
9052+
Equal(t, errs, nil)
9053+
9054+
s = "abc def"
9055+
errs = validate.Var(s, "alphaspace")
9056+
Equal(t, errs, nil)
9057+
9058+
s = " "
9059+
errs = validate.Var(s, "alphaspace")
9060+
Equal(t, errs, nil)
9061+
9062+
s = "abc!"
9063+
errs = validate.Var(s, "alphaspace")
9064+
NotEqual(t, errs, nil)
9065+
AssertError(t, errs, "", "", "", "", "alphaspace")
9066+
9067+
errs = validate.Var(1, "alphaspace")
9068+
NotEqual(t, errs, nil)
9069+
AssertError(t, errs, "", "", "", "", "alphaspace")
9070+
}
9071+
90479072
func TestStructStringValidation(t *testing.T) {
90489073
validate := New()
90499074

0 commit comments

Comments
 (0)