Comments (3)
A possible fix would be to include more faker.cell_phone.formats
in the locale translations for different regions that match valid number structures.
from faker.
Here's a subtle patch for anyone needing valid phone numbers that are genuinely valid.
This overwrites the Faker::PhoneNumber
and will attempt to generate a valid number running through 1000 possibilities until a valid type appears.
# 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
from faker.
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:
https://github.com/faker-ruby/faker/blob/main/lib/helpers/positional_generator.rb
Are you open to opening a PR adding this to the docs? I'm sure others will have the same question.
from faker.
Related Issues (20)
- New generator request: Faker::Company.indian_gst_number HOT 3
- Outdated country names and inconsistencies in en/address.yml HOT 5
- Add benchmarking to faker-ruby HOT 6
- Deprecate Generators with External Calls HOT 3
- Fix/Deprecate `FmaBrotherhood` Generator HOT 2
- Reproducing "Faker::Base#regexify does not work with non-capturing groups or + repeat operator" on v 3.2.1 HOT 2
- `Faker::Time.backward (.forward)` may generate time which is greater (less) than the current time HOT 4
- Fix flaky spec on `TestFakerFile#test_file_name` HOT 6
- Translation missing | en.faker.company.indian_gst_number HOT 6
- Consistent addresses HOT 3
- Drop support for Ruby 2.7 HOT 8
- Fake Zip Codes HOT 6
- v3.2.3 correct? HOT 9
- Faker 3.3.0 generating invalid phone numbers HOT 8
- Invalid Timezone: Atlantic/Cabo_Verde HOT 1
- Random Test Failure: Faker::Omniauth tests fail when generating a name with an apostrophe HOT 8
- Add option to silence deprecation warnings when running the tests HOT 4
- Add id method to Faker::Internet module HOT 3
- Latest version generates invalid email addresses HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from faker.