marmelroy / PhoneNumberKit

A Swift framework for parsing, formatting and validating international phone numbers. Inspired by Google's libphonenumber.
MIT License
5.1k stars 810 forks source link

Not able to validate Phone Number for non-US region on 3.7.9 / 3.7/10 #762

Closed mithleshgomotive closed 4 months ago

mithleshgomotive commented 4 months ago

New Issue Checklist

Steps to reproduce

Basically I was using PhoneNumberKit version 3.3.4 and with this I also had unit-test written. Things were working fine. Few weeks back, I bumped the version of PhoneNumberKit library to 3.7.9 (or now 3.7.10), the same UT was not working for non-US regions.

UT's working or not working, is fine anyway. But I just noticed the region got changed as I'm working from India. The colleague working from US, for them this change doesn't matter and UT kept on working.

The UT was failing for India, Poland basically non-US region.

XCTAssertEqual(delegate.validate(phoneField, data: TextFieldComponentData(title: "", value: "123456789012")), .error(description: "Please use a valid phone number."))

/// Breaking down the above UT code:
/// Test PhoneNumber: 123456789012
/// It should fail ideally but it's passing. Hence above UT failed.

For one of my colleague in Poland region, the other set of UT started to fail (being region specific).

I debugged and found that since we are not passing explicitly defaultRegion code to PhoneNumberKit, it's causing to fail because it's taking user's current region - based on the iPhone's carrier and if not available, the device region.

Screenshot for reference.


The above one is not expected IMO. I am raising this as an issue because the set of UT was working earlier and now with version bump it started to fail.

To fix this issue:

Still I wanted to call this out because change in behaviour. Can you please check this and let us know why there is behavioural change here?

Expected result
XCTAssertEqual(delegate.validate(phoneField, data: TextFieldComponentData(title: "", value: "123456789012")), .error(description: "Please use a valid phone number."))
Actual result

Environment

bguidolim commented 4 months ago

Hey @mithleshgomotive

The main reason why it's working on version 3.3.4 is because it's using a deprecated way to get the default region for iOS 16 and 17 (maybe iOS 15 too), which means that it will always fallback to US as default region.

The recent releases relies on system's region if available. The region is needed in order to parse a phone number that doesn't contain the international code.

If you always have the international code, it will work independently of the region set, if the international code is not available, the library will use whatever is available to try to parse the phone number.

So, with that in mind, you need to update your unit tests to pass a region or the full phone number.

I hope it helps.

mithleshgomotive commented 4 months ago

Thanks for responding & clarifying @bguidolim