Skip to content

Commit

Permalink
Merge pull request #13 from nyaruka/shortcodes
Browse files Browse the repository at this point in the history
Loosen tel URN validation to allow shortcodes
  • Loading branch information
rowanseymour authored May 10, 2018
2 parents cb587f9 + 8cdf327 commit f165005
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/...
```
7 changes: 4 additions & 3 deletions urns/urns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}$`)
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 14 additions & 10 deletions urns/urns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""},
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f165005

Please sign in to comment.