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

phone_number can generate invalid US numbers #24

Closed
haxney opened this issue Jun 26, 2011 · 8 comments
Closed

phone_number can generate invalid US numbers #24

haxney opened this issue Jun 26, 2011 · 8 comments

Comments

@haxney
Copy link

haxney commented Jun 26, 2011

I'm using the phone library to parse phone numbers, and it does not allow invalidly-formatted phone numbers. An example is that US area codes are not allowed to start with 1 (and 0, I believe), but Faker will generate numbers with these invalid area codes. Would it be possible to restrict the range of numbers used in generating area codes for Faker phone numbers?

@darrenterhune
Copy link

@haxney all you need to do here is set Faker::Config.locale to 'en-us' and add in valid us phone number formats to that i18n locale file as it's not yet implemented ;)

@toobulkeh
Copy link
Contributor

👍 for a Phony/Faker crossover.

@chellberg
Copy link

For anyone running into this issue today, you just need Faker::Config.locale = 'en-US' (capitalization matters) - implemented in https://github.com/stympy/faker/blob/master/lib/locales/en-US.yml

@nofxx
Copy link
Contributor

nofxx commented Apr 5, 2016

What I did in an application with heavy phone use was Phony validation, as @toobulkeh mentioned.
Just drop those who fail.

@dorianmariecom
Copy link

dorianmariecom commented Feb 26, 2024

I did:

    phone_number do
      begin
        phone_number = Faker::PhoneNumber.phone_number_with_country_code
        phonelib = Phonelib.parse(phone_number)
      end until phonelib.valid? && phonelib.possible?
      phone_number
    end

@riffraff
Copy link

riffraff commented Mar 5, 2024

for those hitting this issue, I still see it with the en-US locale and recent versions of phonelib

>> Faker::Config.locale = 'en-US'
=> "en-US"
>> 100.times.map { Phonelib.valid? Faker::PhoneNumber.phone_number_with_country_code }.tally
=> {true=>48, false=>52}

the issue seems to be related to extension numbers, as faker will generate stuff with a literal "x" in it, like +1 210.732.4169 x4399.

You can tell phonelib to accept these, and then it should work

>> Phonelib.extension_separate_symbols = %w(ext x # ;)
=> ["ext", "x", "#", ";"]
>> 100.times.map { Phonelib.valid? Faker::PhoneNumber.phone_number_with_country_code }.tally
=> {true=>100}

@dorianmariecom
Copy link

Are those numbers even valid? I will open an issue on phonelib

@dorianmariecom
Copy link

>> Phonelib.extension_separate_symbols = %w(ext x # ;)
=> ["ext", "x", "#", ";"]
>> 100.times.map { Phonelib.valid? Faker::PhoneNumber.phone_number_with_country_code }.tall
y
=> {false=>97, true=>3}

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

8 participants