From cf1b1271efb7cded406427c9195cf396013d170c Mon Sep 17 00:00:00 2001 From: takaaa220 Date: Sun, 8 Dec 2024 06:37:22 +0900 Subject: [PATCH 1/2] add alphaspace validator --- README.md | 1 + baked_in.go | 6 ++++++ doc.go | 6 ++++++ regexes.go | 2 ++ validator_test.go | 25 +++++++++++++++++++++++++ 5 files changed, 40 insertions(+) diff --git a/README.md b/README.md index e3f4e108..667d9a0d 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,7 @@ validate := validator.New(validator.WithRequiredStructEnabled()) | Tag | Description | | - | - | | alpha | Alpha Only | +| alphaspace | Alpha Space | | alphanum | Alphanumeric | | alphanumunicode | Alphanumeric Unicode | | alphaunicode | Alpha Unicode | diff --git a/baked_in.go b/baked_in.go index 2f66c183..0dafbc28 100644 --- a/baked_in.go +++ b/baked_in.go @@ -114,6 +114,7 @@ var ( "fieldcontains": fieldContains, "fieldexcludes": fieldExcludes, "alpha": isAlpha, + "alphaspace": isAlphaSpace, "alphanum": isAlphanum, "alphaunicode": isAlphaUnicode, "alphanumunicode": isAlphanumUnicode, @@ -1761,6 +1762,11 @@ func isAlphanumUnicode(fl FieldLevel) bool { return alphaUnicodeNumericRegex().MatchString(fl.Field().String()) } +// isAlphaSpace is the validation function for validating if the current field's value is a valid alpha value with spaces. +func isAlphaSpace(fl FieldLevel) bool { + return alphaSpaceRegex().MatchString(fl.Field().String()) +} + // isAlphaUnicode is the validation function for validating if the current field's value is a valid alpha unicode value. func isAlphaUnicode(fl FieldLevel) bool { return alphaUnicodeRegex().MatchString(fl.Field().String()) diff --git a/doc.go b/doc.go index c9b1616e..eab98d6c 100644 --- a/doc.go +++ b/doc.go @@ -762,6 +762,12 @@ This validates that a string value contains ASCII alpha characters only Usage: alpha +# Alpha Space + +This validate that a string value contains ASCII alpha characters and spaces only + + Usage: alphaspace + # Alphanumeric This validates that a string value contains ASCII alphanumeric characters only diff --git a/regexes.go b/regexes.go index 871cf7df..c416acc6 100644 --- a/regexes.go +++ b/regexes.go @@ -7,6 +7,7 @@ import ( const ( alphaRegexString = "^[a-zA-Z]+$" + alphaSpaceRegexString = "^[a-zA-Z ]+$" alphaNumericRegexString = "^[a-zA-Z0-9]+$" alphaUnicodeRegexString = "^[\\p{L}]+$" alphaUnicodeNumericRegexString = "^[\\p{L}\\p{N}]+$" @@ -92,6 +93,7 @@ func lazyRegexCompile(str string) func() *regexp.Regexp { var ( alphaRegex = lazyRegexCompile(alphaRegexString) + alphaSpaceRegex = lazyRegexCompile(alphaSpaceRegexString) alphaNumericRegex = lazyRegexCompile(alphaNumericRegexString) alphaUnicodeRegex = lazyRegexCompile(alphaUnicodeRegexString) alphaUnicodeNumericRegex = lazyRegexCompile(alphaUnicodeNumericRegexString) diff --git a/validator_test.go b/validator_test.go index 5eadb250..cb731517 100644 --- a/validator_test.go +++ b/validator_test.go @@ -8990,6 +8990,31 @@ func TestAlpha(t *testing.T) { AssertError(t, errs, "", "", "", "", "alpha") } +func TestAlphaSpace(t *testing.T) { + validate := New() + + s := "abcd" + errs := validate.Var(s, "alphaspace") + Equal(t, errs, nil) + + s = "abc def" + errs = validate.Var(s, "alphaspace") + Equal(t, errs, nil) + + s = " " + errs = validate.Var(s, "alphaspace") + Equal(t, errs, nil) + + s = "abc!" + errs = validate.Var(s, "alphaspace") + NotEqual(t, errs, nil) + AssertError(t, errs, "", "", "", "", "alphaspace") + + errs = validate.Var(1, "alphaspace") + NotEqual(t, errs, nil) + AssertError(t, errs, "", "", "", "", "alphaspace") +} + func TestStructStringValidation(t *testing.T) { validate := New() From da6de8794ce61102912028dd1df34efedf6e8c7f Mon Sep 17 00:00:00 2001 From: takaaa220 Date: Mon, 9 Dec 2024 11:31:50 +0900 Subject: [PATCH 2/2] Fix doc for alpha space --- doc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc.go b/doc.go index eab98d6c..e272628a 100644 --- a/doc.go +++ b/doc.go @@ -764,7 +764,7 @@ This validates that a string value contains ASCII alpha characters only # Alpha Space -This validate that a string value contains ASCII alpha characters and spaces only +This validates that a string value contains ASCII alpha characters and spaces only Usage: alphaspace