From 9d6782aa52aad2ec5f3ea5af63b7dc4bc5192c40 Mon Sep 17 00:00:00 2001 From: Rudresh Amin Date: Fri, 22 Jun 2018 09:48:09 -0700 Subject: [PATCH] Adding a new validator for E164 phone format. (#46) --- README.md | 1 + is/rules.go | 8 ++++++++ is/rules_test.go | 1 + 3 files changed, 10 insertions(+) diff --git a/README.md b/README.md index 5f25bfa..d6ebe98 100644 --- a/README.md +++ b/README.md @@ -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() diff --git a/is/rules.go b/is/rules.go index 7ba342d..73cfb7e 100644 --- a/is/rules.go +++ b/is/rules.go @@ -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 @@ -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])?$`) diff --git a/is/rules_test.go b/is/rules_test.go index fd53ce3..8d3189a 100644 --- a/is/rules_test.go +++ b/is/rules_test.go @@ -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"},