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 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()