Skip to content

Commit

Permalink
Follow the new prefix rules.
Browse files Browse the repository at this point in the history
  • Loading branch information
strotgen committed Aug 20, 2024
1 parent e498592 commit 615d529
Showing 1 changed file with 21 additions and 4 deletions.
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

0 comments on commit 615d529

Please sign in to comment.