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

Invalid GB phone number from Faker::PhoneNumber.phone_number #2898

Closed
TastyPi opened this issue Jan 30, 2024 · 3 comments
Closed

Invalid GB phone number from Faker::PhoneNumber.phone_number #2898

TastyPi opened this issue Jan 30, 2024 · 3 comments

Comments

@TastyPi
Copy link

TastyPi commented Jan 30, 2024

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 to Phonelib.

Additional context

N/A

@kylewelsby
Copy link

A possible fix would be to include more faker.cell_phone.formats in the locale translations for different regions that match valid number structures.

@kylewelsby
Copy link

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

@stefannibrasil
Copy link
Contributor

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.

@thdaraujo thdaraujo closed this as not planned Won't fix, can't repro, duplicate, stale Feb 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants