-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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 3.3.0 generating invalid phone numbers #2922
Comments
Hey @theycallmeswift , thanks for reporting this. Faker does not guarantee that a generated phone number will be valid via Phonelib. If you need to generate a specific format, please take a look at these discussions: |
@thdaraujo makes sense. Can you help me understand what changed that caused this though? I've been banging my head with Git bisect and haven't been able to trace this to anything in the commit history. |
Also, in case it's helpful to anyone in the future, this is an easy thing to add to your # frozen_string_literal: true
module Faker
class PhoneNumber < Base
class << self
def respond_to_missing?(method_name, include_private = false)
method_name.to_s.start_with?('valid_') || super
end
def method_missing(method, *args, &block)
super unless method.to_s.starts_with?('valid_')
# In a sample of 100,000 attempts, it takes an average of 3.6 tries and
# more than 50 is exceptional (0.001%)
max_attempts = 50
attempts = 0
loop do
number = send(method.to_s.gsub('valid_', '').to_sym, *args, &block)
return number if Phonelib.valid? number
attempts += 1
break if attempts >= max_attempts
end
raise "Faker unable to generate valid phone number using #{method} in #{attempts} tries."
end
end
end
end |
I just encountered this same problem. @theycallmeswift thanks for opening this issue. I think what's actually happening here is the result of a legitimate bug with cell phone number generation and this issue shouldn't be closed. There was a change to the file structure of Spot checking the other Further notes from my debugging below. Faker maintains US-specific area codes You can trace a change in behavior around US numbers by inspecting the translation within a console. Here it is working on the released gem 3.2.3 within an app I maintain: # on the released gem 3.2.3 (not the v3.2.3 git tag)
Faker::Config.locale = "en-US"
Faker::PhoneNumber.translate("faker.cell_phone.formats")
# =>
# ["\#{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}",
# "\#{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}"] Here it is broken on the 3.3.0 release and on the v3.2.3 git tag for this repo: # on 3.3.0 (and the v3.2.3 git tag)
Faker::Config.locale = "en-US"
Faker::PhoneNumber.translate("faker.cell_phone.formats")
# => ["###-###-####", "(###) ###-####", "###.###.####", "### ### ####"]
Note that there's a discrepancy between the If you In 31d99d1, the structure of the Here's the before: And the after: The def cell_phone
parse('cell_phone.formats')
end That means 3.3.0 changed Spot checking other locale files indicates that |
I've opened #2924 which should address this. |
In case it helps anyone, see #2924 (comment) for a short-term workaround that doesn't involve generating US numbers with retries. |
oh interesting, thanks for investigating @aprescott ! That makes sense, and you're right, the refactored yaml commit was only added on v3.3. |
thank you @aprescott releasing the fix in a patch release in a bit. |
Describe the bug
Starting with version 3.3.0, Faker is generating invalid US phone numbers.
To Reproduce
Use the reproduction script below to reproduce the issue. If you swap the library version back to 3.2.3, the tests will always pass successfully.
The text was updated successfully, but these errors were encountered: