From 90f125da86f2a33cde2d86f164085c25dca5738c Mon Sep 17 00:00:00 2001 From: Hari Babu Kannan Date: Sat, 1 Nov 2025 10:29:25 +0530 Subject: [PATCH 1/3] Added alphanumspace string validator --- README.md | 1 + baked_in.go | 6 +++++ regexes.go | 2 ++ validator_test.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+) diff --git a/README.md b/README.md index cb5d4194..973bfc67 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,7 @@ validate := validator.New(validator.WithRequiredStructEnabled()) | alpha | Alpha Only | | alphaspace | Alpha Space | | alphanum | Alphanumeric | +| alphanumspace | Alphanumeric Space | | alphanumunicode | Alphanumeric Unicode | | alphaunicode | Alpha Unicode | | ascii | ASCII | diff --git a/baked_in.go b/baked_in.go index 8fd55e77..74f124f3 100644 --- a/baked_in.go +++ b/baked_in.go @@ -120,6 +120,7 @@ var ( "alpha": isAlpha, "alphaspace": isAlphaSpace, "alphanum": isAlphanum, + "alphanumspace": isAlphaNumericSpace, "alphaunicode": isAlphaUnicode, "alphanumunicode": isAlphanumUnicode, "boolean": isBoolean, @@ -1773,6 +1774,11 @@ func isAlphaSpace(fl FieldLevel) bool { return alphaSpaceRegex().MatchString(fl.Field().String()) } +// isAlphaNumericSpace is the validation function for validating if the current field's value is a valid alphanumeric value with spaces. +func isAlphaNumericSpace(fl FieldLevel) bool { + return alphanNumericSpaceRegex().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/regexes.go b/regexes.go index 0b3615f5..2bb965dc 100644 --- a/regexes.go +++ b/regexes.go @@ -9,6 +9,7 @@ const ( alphaRegexString = "^[a-zA-Z]+$" alphaSpaceRegexString = "^[a-zA-Z ]+$" alphaNumericRegexString = "^[a-zA-Z0-9]+$" + alphaNumericSpaceRegexString = "^[a-zA-Z0-9 ]+$" alphaUnicodeRegexString = "^[\\p{L}]+$" alphaUnicodeNumericRegexString = "^[\\p{L}\\p{N}]+$" numericRegexString = "^[-+]?[0-9]+(?:\\.[0-9]+)?$" @@ -95,6 +96,7 @@ func lazyRegexCompile(str string) func() *regexp.Regexp { var ( alphaRegex = lazyRegexCompile(alphaRegexString) alphaSpaceRegex = lazyRegexCompile(alphaSpaceRegexString) + alphanNumericSpaceRegex = lazyRegexCompile(alphaNumericSpaceRegexString) alphaNumericRegex = lazyRegexCompile(alphaNumericRegexString) alphaUnicodeRegex = lazyRegexCompile(alphaUnicodeRegexString) alphaUnicodeNumericRegex = lazyRegexCompile(alphaUnicodeNumericRegexString) diff --git a/validator_test.go b/validator_test.go index 8e969d30..22b32a1c 100644 --- a/validator_test.go +++ b/validator_test.go @@ -9069,6 +9069,73 @@ func TestAlphaSpace(t *testing.T) { AssertError(t, errs, "", "", "", "", "alphaspace") } +func TestAlphaNumericSpace(t *testing.T) { + validate := New() + + s := "abcd 123" + errs := validate.Var(s, "alphanumspace") + Equal(t, errs, nil) + + s = " " + errs = validate.Var(s, "alphanumspace") + Equal(t, errs, nil) + + s = "abc123" + errs = validate.Var(s, "alphanumspace") + Equal(t, errs, nil) + + s = "123" + errs = validate.Var(s, "alphanumspace") + Equal(t, errs, nil) + + s = "abc" + errs = validate.Var(s, "alphanumspace") + Equal(t, errs, nil) + + s = "áçć 123" + errs = validate.Var(s, "alphanumspace") + NotEqual(t, errs, nil) + AssertError(t, errs, "", "", "", "", "alphanumspace") + + s = "日本 123" + errs = validate.Var(s, "alphanumspace") + NotEqual(t, errs, nil) + AssertError(t, errs, "", "", "", "", "alphanumspace") + + s = "abc!" + errs = validate.Var(s, "alphanumspace") + NotEqual(t, errs, nil) + AssertError(t, errs, "", "", "", "", "alphanumspace") + + s = "abc\t123" + errs = validate.Var(s, "alphanumspace") + NotEqual(t, errs, nil) + AssertError(t, errs, "", "", "", "", "alphanumspace") + + s = "abc\n123" + errs = validate.Var(s, "alphanumspace") + NotEqual(t, errs, nil) + AssertError(t, errs, "", "", "", "", "alphanumspace") + + s = "abc-123" + errs = validate.Var(s, "alphanumspace") + NotEqual(t, errs, nil) + AssertError(t, errs, "", "", "", "", "alphanumspace") + + s = "abc🙂123" + errs = validate.Var(s, "alphanumspace") + NotEqual(t, errs, nil) + AssertError(t, errs, "", "", "", "", "alphanumspace") + + errs = validate.Var(1, "alphanumspace") + NotEqual(t, errs, nil) + AssertError(t, errs, "", "", "", "", "alphanumspace") + + errs = validate.Var(1.23, "alphanumspace") + NotEqual(t, errs, nil) + AssertError(t, errs, "", "", "", "", "alphanumspace") +} + func TestStructStringValidation(t *testing.T) { validate := New() From a62cf8d014ac8d6e57d3f05cb11bae77031394b8 Mon Sep 17 00:00:00 2001 From: Hari Babu Kannan Date: Sat, 1 Nov 2025 21:07:06 +0530 Subject: [PATCH 2/3] disabled modernize linter --- .golangci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.golangci.yaml b/.golangci.yaml index dd9c05cc..e89da03d 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -52,3 +52,4 @@ linters: - varnamelen - wrapcheck - wsl + - modernize From fd2447bebf4fcf4c7a3599887fa0c1355463d2ef Mon Sep 17 00:00:00 2001 From: haribabuk113 Date: Sun, 16 Nov 2025 06:22:09 +0530 Subject: [PATCH 3/3] Rebased with master branch, Added description for alphanumspace --- .golangci.yaml | 3 +-- doc.go | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index aea691cd..57e5d830 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -52,5 +52,4 @@ linters: - unparam - varnamelen - wrapcheck - - wsl - - modernize + - wsl \ No newline at end of file diff --git a/doc.go b/doc.go index d8312027..612bcf55 100644 --- a/doc.go +++ b/doc.go @@ -789,6 +789,12 @@ This validates that a string value contains ASCII alphanumeric characters only Usage: alphanum +# Alphanumeric Space + +This validates that a string value contains ASCII alphanumeric characters and spaces only + + Usage: alphanumspace + # Alpha Unicode This validates that a string value contains unicode alpha characters only