Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ linters:
- varnamelen
- wrapcheck
- wsl
- modernize
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is not needed anymore. Please rebase

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rebase is done.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
6 changes: 6 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ var (
"alpha": isAlpha,
"alphaspace": isAlphaSpace,
"alphanum": isAlphanum,
"alphanumspace": isAlphaNumericSpace,
"alphaunicode": isAlphaUnicode,
"alphanumunicode": isAlphanumUnicode,
"boolean": isBoolean,
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions regexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]+)?$"
Expand Down Expand Up @@ -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)
Expand Down
67 changes: 67 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down