Skip to content

Invalid phone number behavioral differences: Between 0.10.2 and 0.10.3 #336

@eudoxa

Description

@eudoxa

Hello. I appreciate the contributors' work on Phonelib.

I'm updating the gem to 0.10.5 and I've noticed a behavioral difference.


OS: Mac Sequoia, Ruby: 3.3.6, Phonelib: 0.10.2 and 0.10.3

Example:
The number("+813000000000") matches Core::GENERAL but does not have a valid type.
https://libphonenumber.appspot.com/phonenumberparser?number=%2B813000000000

# v0.10.3~
> Phonelib.parse("+813000000000").country
=> nil
> Phonelib.parse("+813000000000", "JP").country
=> "JP"
# ~v0.10.2
> Phonelib.parse("+813000000000").country
=> "JP"
> Phonelib.parse("+813000000000", "JP").country
=> "JP"

Q. Which behavior is correct for this gem?


It seems this change originates from v0.10.3:

parsed = parse_single_country(with_replaced_national_prefix(phone, data), data) unless parsed && parsed[key] && parsed[key][:valid].size > 0

The parse_single_country method expects an E.164 format, but the with_replaced_national_prefix method removes the international dialing code, causing it to not match.

Within the with_replaced_national_prefix, if phone is a match with data[Core::NATIONAL_PREFIX_FOR_PARSING], the country code 81 is added back. But, var phone like "8130..." becomes "30...", so it does not match the following regexp:

# data[Core::NATIONAL_PREFIX_FOR_PARSING]
/^(?:(000[259]\d{6})$|(?:(?:003768)0?)|0)/

This seems like a significant change for a minor update, so I thought it might be unintentional.


Additional:
For US numbers, it seems that the phone number transformation is not performed due to the NATIONAL_PREFIX_TRANSFORM_RULE being nil, resulting in the same outcome line 140 and 141.

parsed = parse_single_country(phone, data)
parsed = parse_single_country(with_replaced_national_prefix(phone, data), data) unless parsed && parsed[key] && parsed[key][:valid].size > 0

So, it is possible to get the country even with an invalid number.

# ~v0.10.2 and v0.10.3~ same
Phonelib.parse("+10000000000").country
=> "US"

Phonelib.parse("+10000000000").instance_variable_get("@data")["US"][:valid]
=> []

I look forward to your reply.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions