Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion regexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
hslRegexString = "^hsl\\(\\s*(?:0|[1-9]\\d?|[12]\\d\\d|3[0-5]\\d|360)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*\\)$"
hslaRegexString = "^hsla\\(\\s*(?:0|[1-9]\\d?|[12]\\d\\d|3[0-5]\\d|360)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*,\\s*(?:(?:0.[1-9]*)|[01])\\s*\\)$"
emailRegexString = "^(?:(?:(?:(?:[a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(?:\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|(?:(?:\\x22)(?:(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(?:\\x20|\\x09)+)?(?:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(\\x20|\\x09)+)?(?:\\x22))))@(?:(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
e164RegexString = "^\\+?[1-9]\\d{1,14}$"
e164RegexString = "^\\+?[1-9]\\d{7,14}$"
base32RegexString = "^(?:[A-Z2-7]{8})*(?:[A-Z2-7]{2}={6}|[A-Z2-7]{4}={4}|[A-Z2-7]{5}={3}|[A-Z2-7]{7}=|[A-Z2-7]{8})$"
base64RegexString = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$"
base64URLRegexString = "^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2}==|[A-Za-z0-9-_]{3}=|[A-Za-z0-9-_]{4})$"
Expand Down
112 changes: 58 additions & 54 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8807,28 +8807,32 @@ func TestRgb(t *testing.T) {
func TestE164(t *testing.T) {
validate := New()

s := "+12025550123"
errs := validate.Var(s, "e164")
Equal(t, errs, nil)

s = "+447911123456"
errs = validate.Var(s, "e164")
Equal(t, errs, nil)

s = "0123456789" // invalid: starts with 0
errs = validate.Var(s, "e164")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "e164")

s = "++12025550123" // invalid: double +
errs = validate.Var(s, "e164")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "e164")
tests := []struct {
number string
expected bool
}{
{"+12025550123", true},
{"+447911123456", true},
{"0123456789", false},
{"++12025550123", false},
{"+1 202-555-0123", false},
{"1234567", false},
{"12345678", true},
{"123456", false},
{"0123456789", false},
}

s = "+1 202-555-0123" // invalid: contains spaces or dashes
errs = validate.Var(s, "e164")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "e164")
for _, test := range tests {
t.Run(test.number, func(t *testing.T) {
errs := validate.Var(test.number, "e164")
if test.expected {
Equal(t, errs, nil)
} else {
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "e164")
}
})
}
}

func TestEmail(t *testing.T) {
Expand Down Expand Up @@ -13475,39 +13479,39 @@ func TestBicIso2022FormatValidation(t *testing.T) {
{"SBICKENXX9", "bic", false},
{"SBICKEN13458", "bic", false},
{"SBICKEN", "bic", false},
{"DEUTDEFF", "bic", true}, // 8-char classic (Germany)
{"DEUTDEFF500", "bic", true}, // 11-char with numeric branch
{"A1B2US33", "bic", true}, // digits allowed in 4!c (bank code)
{"1234US33", "bic", true}, // all digits in 4!c (2022)
{"ZZZ1USAA", "bic", true}, // mixed alnum bank + alnum location
{"AB12AE00", "bic", true}, // UAE 8-char
{"AB12AE009Z9", "bic", true}, // UAE 11-char with mixed branch
{"WG11US335AB", "bic", true}, // example-style with digits in branch
{"BNPAFRPP", "bic", true}, // France (BNP Paribas style)
{"BOFAUS3NXXX", "bic", true}, // US with default XXX branch
{"HSBCHKHHXXX", "bic", true}, // Hong Kong, default branch
{"NEDSZAJJ", "bic", true}, // South Africa 8-char
{"BARCGB22", "bic", true}, // GB 8-char
{"BARCGB22XXX", "bic", true}, // GB 11-char with XXX branch
{"0000GB00", "bic", true}, // 4!c all digits + 2!c all digits (allowed)
{"A1B2GB00XXX", "bic", true}, // valid 11-char with numeric location and XXX
{"TATRAEBX", "bic", true}, // UAE 8-char
{"TATRSABX", "bic", true}, // Saudi 8-char
{"TATREGBX", "bic", true}, // Egypt 8-char
{"TATRBHBX", "bic", true}, // Bahrain 8-char
{"DEUTDEFFF", "bic", false}, // 9-char (invalid length)
{"DEUTDEFF5", "bic", false}, // 9-char (invalid length)
{"DEUTDE", "bic", false}, // 6-char (invalid length)
{"DEUTDEFF50", "bic", false}, // 10-char (invalid length)
{"DEUTDEFF5000", "bic", false}, // 12-char (invalid length)
{"deUTDEFF", "bic", false}, // lowercase not allowed
{"DEUTDEfF", "bic", false}, // lowercase in location
{"DEU@DEFF", "bic", false}, // special char in bank
{"ABCD12FF", "bic", false}, // digits in 2!a country (invalid)
{"ABCDDE1-", "bic", false}, // hyphen in location
{"ABCDDE1_", "bic", false}, // underscore in location
{"ABCDDE١٢", "bic", false}, // non-ASCII digits in location
{"DEUTDEFF", "bic", true}, // 8-char classic (Germany)
{"DEUTDEFF500", "bic", true}, // 11-char with numeric branch
{"A1B2US33", "bic", true}, // digits allowed in 4!c (bank code)
{"1234US33", "bic", true}, // all digits in 4!c (2022)
{"ZZZ1USAA", "bic", true}, // mixed alnum bank + alnum location
{"AB12AE00", "bic", true}, // UAE 8-char
{"AB12AE009Z9", "bic", true}, // UAE 11-char with mixed branch
{"WG11US335AB", "bic", true}, // example-style with digits in branch
{"BNPAFRPP", "bic", true}, // France (BNP Paribas style)
{"BOFAUS3NXXX", "bic", true}, // US with default XXX branch
{"HSBCHKHHXXX", "bic", true}, // Hong Kong, default branch
{"NEDSZAJJ", "bic", true}, // South Africa 8-char
{"BARCGB22", "bic", true}, // GB 8-char
{"BARCGB22XXX", "bic", true}, // GB 11-char with XXX branch
{"0000GB00", "bic", true}, // 4!c all digits + 2!c all digits (allowed)
{"A1B2GB00XXX", "bic", true}, // valid 11-char with numeric location and XXX
{"TATRAEBX", "bic", true}, // UAE 8-char
{"TATRSABX", "bic", true}, // Saudi 8-char
{"TATREGBX", "bic", true}, // Egypt 8-char
{"TATRBHBX", "bic", true}, // Bahrain 8-char

{"DEUTDEFFF", "bic", false}, // 9-char (invalid length)
{"DEUTDEFF5", "bic", false}, // 9-char (invalid length)
{"DEUTDE", "bic", false}, // 6-char (invalid length)
{"DEUTDEFF50", "bic", false}, // 10-char (invalid length)
{"DEUTDEFF5000", "bic", false}, // 12-char (invalid length)
{"deUTDEFF", "bic", false}, // lowercase not allowed
{"DEUTDEfF", "bic", false}, // lowercase in location
{"DEU@DEFF", "bic", false}, // special char in bank
{"ABCD12FF", "bic", false}, // digits in 2!a country (invalid)
{"ABCDDE1-", "bic", false}, // hyphen in location
{"ABCDDE1_", "bic", false}, // underscore in location
{"ABCDDE١٢", "bic", false}, // non-ASCII digits in location
}

validate := New()
Expand Down