Skip to content

Commit 71e5030

Browse files
authored
Fix: validation now rejects phone codes starting with +0 (#1476)
## Fixes Or Enhances This PR fixes a bug where the validation incorrectly allowed phone codes starting with +0. A unit test has also been added for E.164 validation. Connected to: #1475 **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 a221118 commit 71e5030

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

regexes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020
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*\\)$"
2121
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*\\)$"
2222
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{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-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$"
2626
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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8734,6 +8734,33 @@ func TestRgb(t *testing.T) {
87348734
AssertError(t, errs, "", "", "", "", "rgb")
87358735
}
87368736

8737+
func TestE164(t *testing.T) {
8738+
validate := New()
8739+
8740+
s := "+12025550123"
8741+
errs := validate.Var(s, "e164")
8742+
Equal(t, errs, nil)
8743+
8744+
s = "+447911123456"
8745+
errs = validate.Var(s, "e164")
8746+
Equal(t, errs, nil)
8747+
8748+
s = "0123456789" // invalid: starts with 0
8749+
errs = validate.Var(s, "e164")
8750+
NotEqual(t, errs, nil)
8751+
AssertError(t, errs, "", "", "", "", "e164")
8752+
8753+
s = "++12025550123" // invalid: double +
8754+
errs = validate.Var(s, "e164")
8755+
NotEqual(t, errs, nil)
8756+
AssertError(t, errs, "", "", "", "", "e164")
8757+
8758+
s = "+1 202-555-0123" // invalid: contains spaces or dashes
8759+
errs = validate.Var(s, "e164")
8760+
NotEqual(t, errs, nil)
8761+
AssertError(t, errs, "", "", "", "", "e164")
8762+
}
8763+
87378764
func TestEmail(t *testing.T) {
87388765
validate := New()
87398766

0 commit comments

Comments
 (0)