Skip to content

Add en-GB ID Numbers (aka National Insurance Numbers) #3032

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions doc/default/id_number.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Comment on lines +55 to +56
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Besides the locale-specific ID Number methods. Faker supports retrieving localised calls to `.valid` and `.invalid`.
Here is an example:
Besides the default ID Number methods, Faker supports localized `.valid` and `.invalid` values. 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"
```
Comment on lines +65 to +73
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### 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"
```
### en-GB
When the locale is set to British English, unformatted'[National Insurance](https://www.gov.uk/national-insurance/your-national-insurance-number)' numbers are generated:
```ruby
Faker::Config.locale = 'en-GB'
Faker::IdNumber.valid #=> "AJ405924A"
Faker::IdNumber.invalid #=> "BG316764W"

Note: Faker generates a subset of all possible legal/illegal national insurance numbers.


3 changes: 3 additions & 0 deletions lib/locales/en-GB.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/test_en_gb_locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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