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

Fix NINO validation for GB #121

Open
wants to merge 4 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
245 changes: 0 additions & 245 deletions src/gb/nino-prefixes.ts

This file was deleted.

14 changes: 14 additions & 0 deletions src/gb/nino.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ describe('gb/nino', () => {
expect(result.isValid && result.compact).toEqual('HH012345D');
});

it('validate:TJ012345D', () => {
// A valid number with a suffix
const result = validate('TJ012345D');

expect(result.isValid && result.compact).toEqual('TJ012345D');
});

it('validate:hh012345d', () => {
// A valid number with lowercase letters
const result = validate('hh012345d');
Expand Down Expand Up @@ -77,4 +84,11 @@ describe('gb/nino', () => {

expect(result.error).toBeInstanceOf(InvalidFormat);
});

it('validate:TO012345D', () => {
// Invalid prefix, valid numeric section, valid suffix
const result = validate('TO012345D');

expect(result.error).toBeInstanceOf(InvalidFormat);
});
});
25 changes: 21 additions & 4 deletions src/gb/nino.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
* The National Insurance Number (NINO) is a nine character identifier used in
* the United Kingdom.
*
* The first two characters are from an approved list of prefixes, the next
* six characters are numbers issued sequentially and the final character is
* The first two characters are all valid except for a few rules described in the source document,
* the next six characters are numbers issued sequentially, and the final character is
* a letter A-D. The suffix may be omitted if it is not known.
*
* Source
* https://www.gov.uk/hmrc-internal-manuals/national-insurance-manual/nim39110
* https://en.wikipedia.org/wiki/National_Insurance_number
*
* PERSON
Expand All @@ -15,7 +16,6 @@
import * as exceptions from '../exceptions';
import { strings } from '../util';
import { Validator, ValidateReturn } from '../types';
import PREFIXES from './nino-prefixes';

function clean(input: string): ReturnType<typeof strings.cleanUnicode> {
return strings.cleanUnicode(input, ' -.');
Expand All @@ -26,11 +26,28 @@ function validLength(value: string): boolean {
return [8, 9].includes(value.length);
}

function isValidPrefix(prefix: string): boolean {
const invalidChars = ["D", "F", "I", "Q", "U", "V"];
const invalidSecondChar = "O";
const invalidPrefixes = ["BG", "GB", "KN", "NK", "NT", "TN", "ZZ"];

if (invalidChars.includes(prefix[0]) || invalidChars.includes(prefix[1])) {
return false;
}

if (prefix[1] === invalidSecondChar) {
return false;
}

return !invalidPrefixes.includes(prefix);
}

const VALID_FORMAT_REGEX = /^([A-Z]{2})\d{6}[A-D]?$/;

function validFormat(value: string): boolean {
const matchData = value.toUpperCase().match(VALID_FORMAT_REGEX);
return !!matchData && PREFIXES.has(matchData[1]);
const prefix = value.slice(0, 2).toUpperCase();
return !!matchData && isValidPrefix(prefix);
}

const impl: Validator = {
Expand Down
Loading