Coder Social home page Coder Social logo

Comments (3)

kylewelsby avatar kylewelsby commented on July 28, 2024

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.

kylewelsby avatar kylewelsby commented on July 28, 2024

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.

stefannibrasil avatar stefannibrasil commented on July 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.