From 41ea916ab231bad450a4b46cbb6a6ccace3d0372 Mon Sep 17 00:00:00 2001 From: Stefan Collier Date: Thu, 5 Dec 2024 18:29:03 +0000 Subject: [PATCH 1/2] Add GB National Insurance Numbers aka ID Numbers In GB, we use [national insurance numbers](https://www.gov.uk/national-insurance/your-national-insurance-number) as our primary citizen identification numbers. These are 9 digit strings that are typically 2 alpha characters followed by 6 numbers and finally a single alpha. There are few combinations are not permitted [as seen here](https://www.gov.uk/hmrc-internal-manuals/national-insurance-manual/nim39110). The `valid` code provided will produce a subset of all legal NI numbers. A regex in the locale file to produce all combinations isn't necessary. --- lib/locales/en-GB.yml | 3 +++ test/test_en_gb_locale.rb | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/locales/en-GB.yml b/lib/locales/en-GB.yml index 96aaa2992e..01d667d8f1 100644 --- a/lib/locales/en-GB.yml +++ b/lib/locales/en-GB.yml @@ -85,6 +85,9 @@ en-GB: - Northern Ireland default_country_code: - GB + id_number: + valid: '/[A-CEGHJ-NOPR-TW-Z][ACEHJLMOPRSW][0-9]{6}[ABCD]/' + invalid: '/(BG|GB|NK|KN|TN|NT|ZZ)[0-9]{6}[E-Z]{1}/' internet: domain_suffix: - co.uk diff --git a/test/test_en_gb_locale.rb b/test/test_en_gb_locale.rb index d12cf30cb9..099295cbca 100644 --- a/test/test_en_gb_locale.rb +++ b/test/test_en_gb_locale.rb @@ -69,4 +69,18 @@ def test_en_gb_postcode_outcode_is_valid assert_match(/^[A-PR-UWYZ][A-HK-Y0-9]/, outcode) assert_match(/\w{1,2}\d{1,2}|\w\d[ABCDEFGHJKPSTUW]|\w\w\d[ABEHMNPRVWXY]/, outcode) end + + def test_en_gb_id_number_valid_is_valid + id_code = Faker::IdNumber.valid + + assert_equal(9, id_code.length) + assert_match(/^[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z][0-9]{6}[A-DFM]$/, id_code) + end + + def test_en_gb_id_number_invalid_is_invalid + id_code = Faker::IdNumber.invalid + + assert_equal(9, id_code.length) + assert_not_match(/^[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z][0-9]{6}[A-DFM]$/, id_code) + end end From ca11234772cd58d65c9a0257e8467979ef711c8c Mon Sep 17 00:00:00 2001 From: Stefan Collier Date: Wed, 8 Jan 2025 18:14:08 +0000 Subject: [PATCH 2/2] Document en-GB Faker::IdNumber locale intricacies --- doc/default/id_number.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/doc/default/id_number.md b/doc/default/id_number.md index 89216c0279..2f34871b01 100644 --- a/doc/default/id_number.md +++ b/doc/default/id_number.md @@ -50,3 +50,25 @@ Faker::IdNumber.danish_id_number(gender: :female) #=> "050390-9980" # Generate a valid French Social Security number (INSEE number) Faker::IdNumber.french_insee_number #=> "22510589696868" ``` + +## ID Number and Locales +Besides the locale-specific ID Number methods. Faker supports retrieving localised calls to `.valid` and `.invalid`. +Here is an example: + +```ruby +Faker::Config.locale = 'fr-FR' +Faker::IdNumber.valid #=> "22510589696868" +``` + +Locales with specific intricacies are as such: + +### en-GB +When provided with British English, unformatted'[National Insurance](https://www.gov.uk/national-insurance/your-national-insurance-number)' numbers are generated. +Note: Faker can only generate a subset of all possible legal/illegal national insurance numbers. + +```ruby +Faker::Config.locale = 'en-GB' +Faker::IdNumber.valid #=> "AJ405924A" +Faker::IdNumber.invalid #=> "BG316764W" +``` +