Skip to content

Commit

Permalink
Adding a new validator for E164 phone format. (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudresh Amin authored and qiangxue committed Jun 22, 2018
1 parent 6679d2e commit 9d6782a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ Below is the whole list of the rules provided by the `is` package:
* `VariableWidth`: validates if a string contains both full-width and half-width characters
* `Base64`: validates if a string is encoded in Base64
* `DataURI`: validates if a string is a valid base64-encoded data URI
* `E164`: validates if a string is a valid E164 phone number (+19251232233)
* `CountryCode2`: validates if a string is a valid ISO3166 Alpha 2 country code
* `CountryCode3`: validates if a string is a valid ISO3166 Alpha 3 country code
* `DialString`: validates if a string is a valid dial string that can be passed to Dial()
Expand Down
8 changes: 8 additions & 0 deletions is/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ var (
Base64 = validation.NewStringRule(govalidator.IsBase64, "must be encoded in Base64")
// DataURI validates if a string is a valid base64-encoded data URI
DataURI = validation.NewStringRule(govalidator.IsDataURI, "must be a Base64-encoded data URI")
// E164 validates if a string is a valid ISO3166 Alpha 2 country code
E164 = validation.NewStringRule(isE164Number, "must be a valid E164 number")
// CountryCode2 validates if a string is a valid ISO3166 Alpha 2 country code
CountryCode2 = validation.NewStringRule(govalidator.IsISO3166Alpha2, "must be a valid two-letter country code")
// CountryCode3 validates if a string is a valid ISO3166 Alpha 3 country code
Expand Down Expand Up @@ -134,6 +136,12 @@ func isDigit(value string) bool {
return reDigit.MatchString(value)
}

func isE164Number(value string) bool {
// E164 regex source: https://stackoverflow.com/a/23299989
reE164 := regexp.MustCompile(`^\+?[1-9]\d{1,14}$`)
return reE164.MatchString(value)
}

func isSubdomain(value string) bool {
// Subdomain regex source: https://stackoverflow.com/a/7933253
reSubdomain := regexp.MustCompile(`^[A-Za-z0-9](?:[A-Za-z0-9\-]{0,61}[A-Za-z0-9])?$`)
Expand Down
1 change: 1 addition & 0 deletions is/rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestAll(t *testing.T) {
{"JSON", JSON, "[1, 2]", "[1, 2,]", "must be in valid JSON format"},
{"ASCII", ASCII, "abc", "aabc", "must contain ASCII characters only"},
{"PrintableASCII", PrintableASCII, "abc", "aabc", "must contain printable ASCII characters only"},
{"E164", E164, "+19251232233", "+00124222333", "must be a valid E164 number"},
{"CountryCode2", CountryCode2, "US", "XY", "must be a valid two-letter country code"},
{"CountryCode3", CountryCode3, "USA", "XYZ", "must be a valid three-letter country code"},
{"DialString", DialString, "localhost.local:1", "localhost.loc:100000", "must be a valid dial string"},
Expand Down

0 comments on commit 9d6782a

Please sign in to comment.