diff --git a/README.md b/README.md index 1fdd385..852cae2 100644 --- a/README.md +++ b/README.md @@ -7,5 +7,5 @@ Common functionality in goflow and courier. You can run all the tests (excluding tests in vendor packages) with: ``` -% go test $(go list ./... | grep -v /vendor/) +% go test github.com/nyaruka/gocommon/... ``` diff --git a/urns/urns.go b/urns/urns.go index c6954a3..380640a 100644 --- a/urns/urns.go +++ b/urns/urns.go @@ -74,6 +74,7 @@ func IsValidScheme(scheme string) bool { } var nonTelCharsRegex = regexp.MustCompile(`[^0-9a-z]`) +var telRegex = regexp.MustCompile(`^\+?[a-zA-Z0-9]{3,16}$`) var twitterHandleRegex = regexp.MustCompile(`^[a-zA-Z0-9_]{1,15}$`) var emailRegex = regexp.MustCompile(`^[^\s@]+@[^\s@]+$`) var viberRegex = regexp.MustCompile(`^[a-zA-Z0-9_=/+]{1,24}$`) @@ -186,10 +187,10 @@ func (u URN) Validate() error { switch scheme { case TelScheme: // validate is possible phone number - _, err := phonenumbers.Parse(path, "") - if err != nil { - return err + if !telRegex.MatchString(path) { + return fmt.Errorf("invalid tel number: %s", path) } + case TwitterScheme: // validate twitter URNs look like handles if !twitterHandleRegex.MatchString(path) { diff --git a/urns/urns_test.go b/urns/urns_test.go index 618b5b9..308dc17 100644 --- a/urns/urns_test.go +++ b/urns/urns_test.go @@ -203,15 +203,15 @@ func TestValidate(t *testing.T) { // valid tel numbers {"tel:+250788383383", ""}, - {"tel:+23761234567", ""}, // old Cameroon format - {"tel:+237661234567", ""}, // new Cameroon format {"tel:+250788383383", ""}, - - {"tel:+250123", ""}, // invalid but parsed we accept it then + {"tel:+250123", ""}, + {"tel:1337", ""}, + {"tel:PRIZES", ""}, // invalid tel numbers - {"tel:0788383383", "invalid country code"}, // no country - {"tel:MTN", "phone number supplied was empty"}, + {"tel:07883 83383", "invalid tel number"}, // can't have spaces + {"tel:12", "invalid tel number"}, // too short + {"tel:12345678901234567", "invalid tel number"}, // too long // twitter handles {"twitter:jimmyjo", ""}, @@ -287,15 +287,19 @@ func TestTelURNs(t *testing.T) { {"+250788383383", "", "tel:+250788383383", false}, {"250788383383", "", "tel:+250788383383", false}, {"(917)992-5253", "US", "tel:+19179925253", false}, + {"(917) 992 - 5253", "US", "tel:+19179925253", false}, {"19179925253", "", "tel:+19179925253", false}, {"+62877747666", "", "tel:+62877747666", false}, {"62877747666", "ID", "tel:+62877747666", false}, {"0877747666", "ID", "tel:+62877747666", false}, {"07531669965", "GB", "tel:+447531669965", false}, - {"12345", "RW", "", true}, - {"0788383383", "", "", true}, - {"0788383383", "ZZ", "", true}, - {"MTN", "RW", "", true}, + {"12345", "RW", "tel:12345", false}, + {"0788383383", "", "tel:0788383383", false}, + {"0788383383", "ZZ", "tel:0788383383", false}, + {"PRIZES", "RW", "tel:prizes", false}, + {"PRIZES!", "RW", "tel:prizes", false}, + {"1", "RW", "", true}, + {"123456789012345678901234567890", "RW", "", true}, } for _, tc := range testCases {