-
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
Invalid GB phone number from Faker::PhoneNumber.phone_number
#2898
Comments
A possible fix would be to include more |
Here's a subtle patch for anyone needing valid phone numbers that are genuinely valid. This overwrites the # frozen_string_literal: true
module Faker
class PhoneNumber < Base
class << self
# Generates a valid phone number.
#
# Overwrites the original Faker::PhoneNumber.phone_number method to ensure that the generated phone number is valid.
#
# @return [String]
# @example
# Faker::PhoneNumber.phone_number #=> "397.693.1309"
def phone_number
generate_valid_number(:fixed_line).national
end
def phone_number_with_country_code
generate_valid_number(:fixed_line).full_e164
end
# Generates a valid cell phone number.
#
# Overwrites the original Faker::PhoneNumber.cell_phone method to ensure that the generated cell phone number is valid.
#
# @return [String]
# @example
# Faker::PhoneNumber.cell_phone #=> "397.693.1309"
def cell_phone
generate_valid_number(:mobile).national
end
def cell_phone_with_country_code
generate_valid_number(:mobile).full_e164
end
def country_code
raise "Unable to determine country code for #{iso_country_code}" if phone_data_for_country.nil?
"+" + phone_data_for_country&.dig(:country_code)
end
private
def generate_valid_number(type = :fixed_line)
example = phone_data_for_country&.dig(:types, type, :example_number)
max_attempts = 1000
attempts = 0
number = nil
loop do
number = Faker::Number.number(digits: example.length)
parsed = Phonelib.parse(number, iso_country_code)
return parsed if parsed.type == type
return parsed if parsed.type == :fixed_or_mobile && ["CA", "US"].include?(iso_country_code) # unable to generate a valid type of number for these countries
attempts += 1
break if attempts >= max_attempts
end
puts "Unable to generate valid phone number after #{max_attempts} attempts, expected #{number} to be valid #{type} for #{iso_country_code}"
Phonelib.parse(example, iso_country_code)
end
def iso_country_code
(Faker::Config.locale.to_s.split("-").last || Faker::Address.fetch("address.default_country_code")).upcase
end
def phone_data_for_country
Phonelib.phone_data[iso_country_code]
end
end
end
end |
This is an expected behaviour. We don't generate valid phone numbers because we don't support Phonelib as we generate random values. One alternative is to use the PositionalGenerator to generate the format you need: Are you open to opening a PR adding this to the docs? I'm sure others will have the same question. |
Describe the bug
Faker generated the following phone number with GB locale:
0825 311 9348
This is invalid according to
Phonelib
, though I haven't yet figured out why.To Reproduce
Not sure how to reproduce reliably in a simple script, I'd need to find a random number seed in a haystack.
Expected behavior
All phone numbers produced by
Faker::PhoneNumber.phone_number
are valid according toPhonelib
.Additional context
N/A
The text was updated successfully, but these errors were encountered: