Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faker::PhoneNumber.cell_phone not enforcing locale #499

Closed
mrkgrandjean opened this issue Jan 20, 2016 · 9 comments
Closed

Faker::PhoneNumber.cell_phone not enforcing locale #499

mrkgrandjean opened this issue Jan 20, 2016 · 9 comments

Comments

@mrkgrandjean
Copy link

I've noticed that when you use the cell phone method after setting the locale, it does not enforce the area code rules. notice in the code sample below, the first result is area code, "126" which is invalid.

Faker::Config.locale = 'en-US'
10.times map{ Faker::PhoneNumber.cell_phone }
#=>  [ "1-126-339-8245",
#=> "187.000.3075",
#=> "(956) 743-8134",
#=> "227.630.6904",
#=> "(294) 316-8197",
#=> "(798) 776-1679",
#=> "129.322.0758",
#=> "656-980-4693",
#=> "153-403-7461",
#=> "313.683.7081" ]
@systemnate
Copy link

I can't seem to reproduce this issue.

First, I require faker and then set the locale to en-US. I then made a loop that runs 10,000 times (and even tried it multiple times) and store the result of called Faker::PhoneNumber.cell_phone into a variable called num. I then use a regular expression to search for the first occurrence of 3 digits (\d{3}) then inspect the first match and first character checking for a "1". If it is found, it outputs the full phone number.

2.3.0 :001 > require 'faker'
 => true 
2.3.0 :002 > Faker::Config.locale = "en-US"
 => "en-US" 
2.3.0 :003 > 10000.times do
2.3.0 :004 >     num = Faker::PhoneNumber.cell_phone
2.3.0 :005?>     if num.match(/\d{3}/)[0][0] == "1"
2.3.0 :006?>       puts num
2.3.0 :007?>     end
2.3.0 :008?>   end
 => 10000 
2.3.0 :009 > 

I also inspected the code: phone_number.rb and faker.rb. In phone_number.rb, the code for cell_phone and phone_number are almost identical...it's just that cell_phone has different (less) formats to not include extensions and the such. Both cell_phone and phone_number call phone_number.area_code:

      # US only
      def area_code
        begin
          fetch('phone_number.area_code')
        rescue I18n::MissingTranslationData
          nil
        end
      end

which calls faker.fetch which takes a "sample" (random element from the Array). This Array contains the following values:

area_code: ["201", "202", "203", "205", "206", "207", "208", "209", "210", "212", "213", "214", "215", "216", "217", "218", "219", "224", "225", "226", "228", "229", "231", "234", "239", "240", "248", "251", "252", "253", "254", "256", "260", "262", "267", "269", "270", "276", "281", "301", "302", "303", "304", "305", "307", "308", "309", "310", "312", "313", "314", "315", "316", "317", "318", "319", "320", "321", "323", "330", "334", "336", "337", "339", "347", "351", "352", "360", "361", "386", "401", "402", "404", "405", "406", "407", "408", "409", "410", "412", "413", "414", "415", "417", "419", "423", "424", "425", "434", "435", "440", "443", "469", "478", "479", "480", "484", "501", "502", "503", "504", "505", "507", "508", "509", "510", "512", "513", "515", "516", "517", "518", "520", "530", "540", "541", "551", "559", "561", "562", "563", "567", "570", "571", "573", "574", "580", "585", "586", "601", "602", "603", "605", "606", "607", "608", "609", "610", "612", "614", "615", "616", "617", "618", "619", "620", "623", "626", "630", "631", "636", "641", "646", "650", "651", "660", "661", "662", "678", "682", "701", "702", "703", "704", "706", "707", "708", "712", "713", "714", "715", "716", "717", "718", "719", "720", "724", "727", "731", "732", "734", "740", "754", "757", "760", "763", "765", "770", "772", "773", "774", "775", "781", "785", "786", "801", "802", "803", "804", "805", "806", "808", "810", "812", "813", "814", "815", "816", "817", "818", "828", "830", "831", "832", "843", "845", "847", "848", "850", "856", "857", "858", "859", "860", "862", "863", "864", "865", "870", "878", "901", "903", "904", "906", "907", "908", "909", "910", "912", "913", "914", "915", "916", "917", "918", "919", "920", "925", "928", "931", "936", "937", "940", "941", "947", "949", "952", "954", "956", "970", "971", "972", "973", "978", "979", "980", "985", "989"]

@systemnate
Copy link

  • - phone_number and cell_phone do not actually call area_code...it calls a format which is defined in en-us.yml. For cell_phone the formats are:
    cell_phone:
      formats:
        - "#{PhoneNumber.area_code}-#{PhoneNumber.exchange_code}-#{PhoneNumber.subscriber_number}"
        - "(#{PhoneNumber.area_code}) #{PhoneNumber.exchange_code}-#{PhoneNumber.subscriber_number}"
        - "1-#{PhoneNumber.area_code}-#{PhoneNumber.exchange_code}-#{PhoneNumber.subscriber_number}"
        - "#{PhoneNumber.area_code}.#{PhoneNumber.exchange_code}.#{PhoneNumber.subscriber_number}"
        - "#{PhoneNumber.area_code}-#{PhoneNumber.exchange_code}-#{PhoneNumber.subscriber_number}"
        - "(#{PhoneNumber.area_code}) #{PhoneNumber.exchange_code}-#{PhoneNumber.subscriber_number}"
        - "1-#{PhoneNumber.area_code}-#{PhoneNumber.exchange_code}-#{PhoneNumber.subscriber_number}"
        - "#{PhoneNumber.area_code}.#{PhoneNumber.exchange_code}.#{PhoneNumber.subscriber_number}"

This doesn't change my investigation, just wanted to me a little more clear.

@ManickYoj
Copy link

I haven't really tried to debug why, but the code you used to search does not return any results for me.

10000.times do
num = Faker::PhoneNumber.cell_phone
    if num.match(/\d{3}/)[0][0] == "1"
        puts num
    end
end

However, a simple

100.times do
    num = Faker::PhoneNumber.cell_phone
    p num
end

suffices to confirm the issue for me returning, eg:

"(105) 521-9454"
"135-160-6048"
"189.362.7790"
"(136) 514-2760"

@ManickYoj
Copy link

ManickYoj commented Sep 20, 2017

Looks like the problem is that the default locale is en, not en-US.

The en config loads the confusingly placed lib/locales/en/phone_number.yml which, in its entirety, reads:

en:
  faker:
    phone_number:
      formats: ['###-###-####', '(###) ###-####', '1-###-###-####', '###.###.####', '###-###-####', '(###) ###-####', '1-###-###-####', '###.###.####', '###-###-#### x###', '(###) ###-#### x###', '1-###-###-#### x###', '###.###.#### x###', '###-###-#### x####', '(###) ###-#### x####', '1-###-###-#### x####', '###.###.#### x####', '###-###-#### x#####', '(###) ###-#### x#####', '1-###-###-#### x#####', '###.###.#### x#####']
    cell_phone:
      formats: ['###-###-####', '(###) ###-####', '1-###-###-####', '###.###.####']

Which allows any number as the first in an area code.

This suggests the fix is setting your locale to en-US.

@ManickYoj
Copy link

Looks like this a duplicate issue of #24 . Suggest closing in favor of that one.

@mrkgrandjean
Copy link
Author

I see. Looks like with the locale set this issue is not an issue.... closing.

@vbrazo
Copy link
Member

vbrazo commented Jun 20, 2019

which faker version are you using? @serhiiperfectial could you provide a sample application?

@vbrazo
Copy link
Member

vbrazo commented Jun 20, 2019

@serhiiperfectial Your piece of code isn't ruby. Are you sure that you're in the correct repo?

@serhiiperfectial
Copy link

serhiiperfectial commented Jun 20, 2019

@serhiiperfectial Your piece of code isn't ruby. Are you sure that you're in the correct repo?

Yeah, sorry. It's Java code... and wrong project. Sorry. I'll direct my question to the one that I'm using.

Sorry again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants