Skip to content

Commit 3de3f9c

Browse files
Merge pull request #3032 from stefanjcollier/add-en-GB-id-numbers
Add en-GB ID Numbers (aka National Insurance Numbers)
2 parents 3d94071 + 86113bd commit 3de3f9c

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

doc/default/id_number.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,24 @@ Faker::IdNumber.danish_id_number(gender: :female) #=> "050390-9980"
5050
# Generate a valid French Social Security number (INSEE number)
5151
Faker::IdNumber.french_insee_number #=> "22510589696868"
5252
```
53+
54+
## ID Number and Locales
55+
56+
Besides the default ID numbers, faker supports localized `.valid` and `.invalid` values:
57+
58+
```ruby
59+
Faker::Config.locale = 'fr-FR'
60+
Faker::IdNumber.valid #=> "22510589696868"
61+
```
62+
63+
Locales with specific intricacies are as such:
64+
65+
### en-GB
66+
67+
When the locale is set to British English, unformatted [National Insurance](https://www.gov.uk/national-insurance/your-national-insurance-number) numbers are generated:
68+
69+
```ruby
70+
Faker::Config.locale = 'en-GB'
71+
Faker::IdNumber.valid #=> "AJ405924A"
72+
Faker::IdNumber.invalid #=> "BG316764W"
73+

lib/locales/en-GB.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ en-GB:
8585
- Northern Ireland
8686
default_country_code:
8787
- GB
88+
id_number:
89+
valid: '/[A-CEGHJ-NOPR-TW-Z][ACEHJLMOPRSW][0-9]{6}[ABCD]/'
90+
invalid: '/(BG|GB|NK|KN|TN|NT|ZZ)[0-9]{6}[E-Z]{1}/'
8891
internet:
8992
domain_suffix:
9093
- co.uk

test/test_en_gb_locale.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,18 @@ def test_en_gb_postcode_outcode_is_valid
6969
assert_match(/^[A-PR-UWYZ][A-HK-Y0-9]/, outcode)
7070
assert_match(/\w{1,2}\d{1,2}|\w\d[ABCDEFGHJKPSTUW]|\w\w\d[ABEHMNPRVWXY]/, outcode)
7171
end
72+
73+
def test_en_gb_id_number_valid
74+
id_code = Faker::IdNumber.valid
75+
76+
assert_equal(9, id_code.length)
77+
assert_match(/^[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z][0-9]{6}[A-DFM]$/, id_code)
78+
end
79+
80+
def test_en_gb_id_number_invalid
81+
id_code = Faker::IdNumber.invalid
82+
83+
assert_equal(9, id_code.length)
84+
assert_not_match(/^[A-CEGHJ-PR-TW-Z][A-CEGHJ-NPR-TW-Z][0-9]{6}[A-DFM]$/, id_code)
85+
end
7286
end

0 commit comments

Comments
 (0)