Skip to content

Commit

Permalink
Merge pull request #121 from strotgen/main
Browse files Browse the repository at this point in the history
Fix NINO validation for GB
  • Loading branch information
koblas authored Nov 16, 2024
2 parents d22fccd + d3ffe82 commit 6a84d84
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 249 deletions.
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

0 comments on commit 6a84d84

Please sign in to comment.