Skip to content

Commit

Permalink
Add workaround for numbers that pass urns.ParseNumber but fail phone …
Browse files Browse the repository at this point in the history
…URN valdiation
  • Loading branch information
rowanseymour committed Aug 21, 2024
1 parent 13265f7 commit 08275fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion utils/phone.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ func ParsePhoneNumber(s string, country i18n.Country) string {
return ""
}

return formatted
// TODO there is a problem in urns package where a number +810000000977123456 is considered valid because it allows
// unlimited zeros between the country code and the number... but our validation for phone URNs thinks it's too
// long, so we validate explicitly here
urn, err := urns.New(urns.Phone, formatted)
if err != nil {
return ""
}

return urn.Path()
}

// FindPhoneNumbers finds phone numbers anywhere in the given string
Expand Down
1 change: 1 addition & 0 deletions utils/phone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestParsePhoneNumber(t *testing.T) {
assert.Equal(t, "", utils.ParsePhoneNumber("tel = +250788383383", "RW"))
assert.Equal(t, "", utils.ParsePhoneNumber("Hi my number is +250788383383 thanks", "RW"))
assert.Equal(t, "", utils.ParsePhoneNumber("Hi my phone is (202) 456-1111 thanks", "US"))
assert.Equal(t, "", utils.ParsePhoneNumber("+810000000977123456", "CD"))

assert.Equal(t, "+250788383383", utils.ParsePhoneNumber("+250788383383", ""))
assert.Equal(t, "+250788383383", utils.ParsePhoneNumber("0788 383 383", "RW"))
Expand Down

0 comments on commit 08275fa

Please sign in to comment.