Skip to content

Commit e8627a1

Browse files
authored
fix: Revert min limit of e164 regex (#1516)
## Fixes Or Enhances Although the E164 spec technically would allow for numbers with only one digit after the country code, we used to expect atleast 7 digits, since that is what is way more practical. In fix for E164 to disallow leading 0 in country codes, we lowered said limit to 1 digit. This broke things for peoples as numbers like `123456` became valid. Reverting back to the original limit of 7 digits, is practical, and probably the expected behavior out of this validator. Related #1515 **Make sure that you've checked the boxes below before you submit PR:** - [x] Tests exist or have been written that cover this particular change. @go-playground/validator-maintainers
1 parent 65b1bcc commit e8627a1

File tree

2 files changed

+59
-55
lines changed

2 files changed

+59
-55
lines changed

regexes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const (
2121
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*\\)$"
2222
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*\\)$"
2323
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-\\x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2-7]{7}=|[A-Z2-7]{8})$"
2626
base64RegexString = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$"
2727
base64URLRegexString = "^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2}==|[A-Za-z0-9-_]{3}=|[A-Za-z0-9-_]{4})$"

validator_test.go

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8901,28 +8901,32 @@ func TestRgb(t *testing.T) {
89018901
func TestE164(t *testing.T) {
89028902
validate := New()
89038903

8904-
s := "+12025550123"
8905-
errs := validate.Var(s, "e164")
8906-
Equal(t, errs, nil)
8907-
8908-
s = "+447911123456"
8909-
errs = validate.Var(s, "e164")
8910-
Equal(t, errs, nil)
8911-
8912-
s = "0123456789" // invalid: starts with 0
8913-
errs = validate.Var(s, "e164")
8914-
NotEqual(t, errs, nil)
8915-
AssertError(t, errs, "", "", "", "", "e164")
8916-
8917-
s = "++12025550123" // invalid: double +
8918-
errs = validate.Var(s, "e164")
8919-
NotEqual(t, errs, nil)
8920-
AssertError(t, errs, "", "", "", "", "e164")
8904+
tests := []struct {
8905+
number string
8906+
expected bool
8907+
}{
8908+
{"+12025550123", true},
8909+
{"+447911123456", true},
8910+
{"0123456789", false},
8911+
{"++12025550123", false},
8912+
{"+1 202-555-0123", false},
8913+
{"1234567", false},
8914+
{"12345678", true},
8915+
{"123456", false},
8916+
{"0123456789", false},
8917+
}
89218918

8922-
s = "+1 202-555-0123" // invalid: contains spaces or dashes
8923-
errs = validate.Var(s, "e164")
8924-
NotEqual(t, errs, nil)
8925-
AssertError(t, errs, "", "", "", "", "e164")
8919+
for _, test := range tests {
8920+
t.Run(test.number, func(t *testing.T) {
8921+
errs := validate.Var(test.number, "e164")
8922+
if test.expected {
8923+
Equal(t, errs, nil)
8924+
} else {
8925+
NotEqual(t, errs, nil)
8926+
AssertError(t, errs, "", "", "", "", "e164")
8927+
}
8928+
})
8929+
}
89268930
}
89278931

89288932
func TestEmail(t *testing.T) {
@@ -13569,39 +13573,39 @@ func TestBicIso2022FormatValidation(t *testing.T) {
1356913573
{"SBICKENXX9", "bic", false},
1357013574
{"SBICKEN13458", "bic", false},
1357113575
{"SBICKEN", "bic", false},
13572-
{"DEUTDEFF", "bic", true}, // 8-char classic (Germany)
13573-
{"DEUTDEFF500", "bic", true}, // 11-char with numeric branch
13574-
{"A1B2US33", "bic", true}, // digits allowed in 4!c (bank code)
13575-
{"1234US33", "bic", true}, // all digits in 4!c (2022)
13576-
{"ZZZ1USAA", "bic", true}, // mixed alnum bank + alnum location
13577-
{"AB12AE00", "bic", true}, // UAE 8-char
13578-
{"AB12AE009Z9", "bic", true}, // UAE 11-char with mixed branch
13579-
{"WG11US335AB", "bic", true}, // example-style with digits in branch
13580-
{"BNPAFRPP", "bic", true}, // France (BNP Paribas style)
13581-
{"BOFAUS3NXXX", "bic", true}, // US with default XXX branch
13582-
{"HSBCHKHHXXX", "bic", true}, // Hong Kong, default branch
13583-
{"NEDSZAJJ", "bic", true}, // South Africa 8-char
13584-
{"BARCGB22", "bic", true}, // GB 8-char
13585-
{"BARCGB22XXX", "bic", true}, // GB 11-char with XXX branch
13586-
{"0000GB00", "bic", true}, // 4!c all digits + 2!c all digits (allowed)
13587-
{"A1B2GB00XXX", "bic", true}, // valid 11-char with numeric location and XXX
13588-
{"TATRAEBX", "bic", true}, // UAE 8-char
13589-
{"TATRSABX", "bic", true}, // Saudi 8-char
13590-
{"TATREGBX", "bic", true}, // Egypt 8-char
13591-
{"TATRBHBX", "bic", true}, // Bahrain 8-char
13592-
13593-
{"DEUTDEFFF", "bic", false}, // 9-char (invalid length)
13594-
{"DEUTDEFF5", "bic", false}, // 9-char (invalid length)
13595-
{"DEUTDE", "bic", false}, // 6-char (invalid length)
13596-
{"DEUTDEFF50", "bic", false}, // 10-char (invalid length)
13597-
{"DEUTDEFF5000", "bic", false}, // 12-char (invalid length)
13598-
{"deUTDEFF", "bic", false}, // lowercase not allowed
13599-
{"DEUTDEfF", "bic", false}, // lowercase in location
13600-
{"DEU@DEFF", "bic", false}, // special char in bank
13601-
{"ABCD12FF", "bic", false}, // digits in 2!a country (invalid)
13602-
{"ABCDDE1-", "bic", false}, // hyphen in location
13603-
{"ABCDDE1_", "bic", false}, // underscore in location
13604-
{"ABCDDE١٢", "bic", false}, // non-ASCII digits in location
13576+
{"DEUTDEFF", "bic", true}, // 8-char classic (Germany)
13577+
{"DEUTDEFF500", "bic", true}, // 11-char with numeric branch
13578+
{"A1B2US33", "bic", true}, // digits allowed in 4!c (bank code)
13579+
{"1234US33", "bic", true}, // all digits in 4!c (2022)
13580+
{"ZZZ1USAA", "bic", true}, // mixed alnum bank + alnum location
13581+
{"AB12AE00", "bic", true}, // UAE 8-char
13582+
{"AB12AE009Z9", "bic", true}, // UAE 11-char with mixed branch
13583+
{"WG11US335AB", "bic", true}, // example-style with digits in branch
13584+
{"BNPAFRPP", "bic", true}, // France (BNP Paribas style)
13585+
{"BOFAUS3NXXX", "bic", true}, // US with default XXX branch
13586+
{"HSBCHKHHXXX", "bic", true}, // Hong Kong, default branch
13587+
{"NEDSZAJJ", "bic", true}, // South Africa 8-char
13588+
{"BARCGB22", "bic", true}, // GB 8-char
13589+
{"BARCGB22XXX", "bic", true}, // GB 11-char with XXX branch
13590+
{"0000GB00", "bic", true}, // 4!c all digits + 2!c all digits (allowed)
13591+
{"A1B2GB00XXX", "bic", true}, // valid 11-char with numeric location and XXX
13592+
{"TATRAEBX", "bic", true}, // UAE 8-char
13593+
{"TATRSABX", "bic", true}, // Saudi 8-char
13594+
{"TATREGBX", "bic", true}, // Egypt 8-char
13595+
{"TATRBHBX", "bic", true}, // Bahrain 8-char
13596+
13597+
{"DEUTDEFFF", "bic", false}, // 9-char (invalid length)
13598+
{"DEUTDEFF5", "bic", false}, // 9-char (invalid length)
13599+
{"DEUTDE", "bic", false}, // 6-char (invalid length)
13600+
{"DEUTDEFF50", "bic", false}, // 10-char (invalid length)
13601+
{"DEUTDEFF5000", "bic", false}, // 12-char (invalid length)
13602+
{"deUTDEFF", "bic", false}, // lowercase not allowed
13603+
{"DEUTDEfF", "bic", false}, // lowercase in location
13604+
{"DEU@DEFF", "bic", false}, // special char in bank
13605+
{"ABCD12FF", "bic", false}, // digits in 2!a country (invalid)
13606+
{"ABCDDE1-", "bic", false}, // hyphen in location
13607+
{"ABCDDE1_", "bic", false}, // underscore in location
13608+
{"ABCDDE١٢", "bic", false}, // non-ASCII digits in location
1360513609
}
1360613610

1360713611
validate := New()

0 commit comments

Comments
 (0)