PhoneNumberKit: Wrong validation for Canada number when validating it agains US region.
Hi,
POD Version - 3.3.4
I have noticed that the API PhoneNumberKit.isValidPhoneNumber(_:withRegion:ignoreType:) is returning incorrect true value when used to lookup if a Canadian number is valid for the US region. In the case where validating a CA (Canada) number to US validation, it should return false but it returns true. Here is an example:
let phoneNumberKit = PhoneNumberKit()
let isCanadaNumberValid = phoneNumberKit.isValidPhoneNumber("14164431000", withRegion: "CA", ignoreType: true) /// returns true
let isUSNumberValid = phoneNumberKit.isValidPhoneNumber("14164431000", withRegion: "US", ignoreType: true) /// returns true
It should not be possible that the same number is valid for both US and CA region.
As a workaround I have found this approach of parsing the phone and then manually validating it using PhoneNumberKit.parse(_:withRegion:ignoreType:):
func isValid(phoneNumber: String, withRegion region: String) -> Bool {
do {
let phone = try phoneNumberKit.parse(phoneNumber, withRegion: region)
let phoneNumberCountryCode = phoneNumberKit.getRegionCode(of: phone)
return phoneNumberCountryCode == region
} catch {
return false
}
}
let isCanadaNumberValid = isValid("14164431000", withRegion: "US") /// returns false
let isUSNumberValid = isValid("14164431000", withRegion: "CA") /// returns true
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 15 (3 by maintainers)
Hey @VladimirAmiorkov
The library is not misbehaving in this case, as you can see in the links I’ve sent you,
isValidNumberreturnstruefor both (US and CA).The check per region is just “not implemented”, but since you already have a solution for this, you can add it as an additional check and create a PR.
I would love to review and merge it. 😃
I think this is because of the fallback function that tries to parse the phone number from different regions with the same international code. I’m looking into this issue already, maybe I should add an option
strictwhen validating phone numbers, if set astrue, ignore the fallback attempt.