daddyz / phonelib

Ruby gem for phone validation and formatting using google libphonenumber library data
MIT License
1.08k stars 131 forks source link

Different validation result if providing the country to the parse method (BR) #309

Closed jonathanhamel4 closed 2 months ago

jonathanhamel4 commented 4 months ago

When parsing mobile phone numbers in Brazil in the area code 55, the results are inconsistent.

For example this works just fine:

Phonelib.parse('+55 55 96722 8964')
=>
#<Phonelib::Phone:0x0000000103fadfd0
 @data=
  {"BR"=>
    {:id=>"BR",
     :country_code=>"55",
     :international_prefix=>"00(?:1[245]|2[1-35]|31|4[13]|[56]5|99)",
     :national_prefix=>"0",
     :national_prefix_for_parsing=>"(?:0|90)(?:(1[245]|2[1-35]|31|4[13]|[56]5|99)(\\d{10,11}))?",
     :national_prefix_transform_rule=>"\\2",
     :mobile_number_portable_region=>"true",
     :double_prefix=>true,
     :national=>"61967228964",
     :format=>{:pattern=>"(\\d{2})(\\d{5})(\\d{4})", :national_prefix_formatting_rule=>"($FG)", :carrier_code_formatting_rule=>"$NP $CC ($FG)", :leading_digits=>"[16][1-9]|[2-57-9]", :format=>"$1 $2-$3"},
     :valid=>[:mobile],
     :possible=>[:mobile]}},

but this results in a not valid number

Phonelib.parse('+55 55 96722 8964', 'BR')
 =>
#<Phonelib::Phone:0x00000001048cf8c0
 @data=
  {"BR"=>
    {:id=>"BR",
     :country_code=>"55",
     :international_prefix=>"00(?:1[245]|2[1-35]|31|4[13]|[56]5|99)",
     :national_prefix=>"0",
     :national_prefix_for_parsing=>"(?:0|90)(?:(1[245]|2[1-35]|31|4[13]|[56]5|99)(\\d{10,11}))?",
     :national_prefix_transform_rule=>"\\2",
     :mobile_number_portable_region=>"true",
     :double_prefix=>true,
     :national=>"967228964",
     :format=>{:pattern=>"(\\d{5})(\\d{4})", :leading_digits=>"9", :format=>"$1-$2", :intl_format=>"NA"},
     :valid=>[],
     :possible=>[:premium_rate, :toll_free]}},

If I try another number, for example in area code 61 like '+55 61 96722 8964', this number validates just fine with and without the country code given to it.

Seems like this validates just fine with libPhoneNumber

Screenshot 2024-07-30 at 8 28 27 AM
daddyz commented 2 months ago

hi @jonathanhamel4, thanks for reporting. Fixed in v0.9.2