From e146e541e8b8d9c9dfdfecf616b703b661036d28 Mon Sep 17 00:00:00 2001 From: Alfonso Strotgen Date: Tue, 20 Aug 2024 09:17:51 -0600 Subject: [PATCH 1/4] Add new test cases for NINO validation. --- src/gb/nino.spec.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/gb/nino.spec.ts b/src/gb/nino.spec.ts index 767f0409..28a8fcf7 100644 --- a/src/gb/nino.spec.ts +++ b/src/gb/nino.spec.ts @@ -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'); @@ -77,4 +84,11 @@ describe('gb/nino', () => { expect(result.error).toBeInstanceOf(InvalidFormat); }); + + it('validate:HO012345E', () => { + // Valid prefix, valid numeric section, invalid suffix + const result = validate('HH012345E'); + + expect(result.error).toBeInstanceOf(InvalidFormat); + }); }); From e498592ec8bdbba2638328381fa3b27483fdc809 Mon Sep 17 00:00:00 2001 From: Alfonso Strotgen Date: Tue, 20 Aug 2024 09:18:34 -0600 Subject: [PATCH 2/4] Remove the prefixes file, not needed. --- src/gb/nino-prefixes.ts | 245 ---------------------------------------- 1 file changed, 245 deletions(-) delete mode 100644 src/gb/nino-prefixes.ts diff --git a/src/gb/nino-prefixes.ts b/src/gb/nino-prefixes.ts deleted file mode 100644 index 86918a75..00000000 --- a/src/gb/nino-prefixes.ts +++ /dev/null @@ -1,245 +0,0 @@ -// Source: https://webarchive.nationalarchives.gov.uk/ukgwa/20120207170042/http://www.hmrc.gov.uk/softwaredevelopers/rti/mig-rti-nvreq.pdf -// (Page 46) -const PREFIXES = new Set([ - 'AA', - 'AB', - 'AE', - 'AH', - 'AK', - 'AL', - 'AM', - 'AP', - 'AR', - 'AS', - 'AT', - 'AW', - 'AX', - 'AY', - 'AZ', - 'BA', - 'BB', - 'BE', - 'BH', - 'BK', - 'BL', - 'BM', - 'BT', - 'CA', - 'CB', - 'CE', - 'CH', - 'CK', - 'CL', - 'CR', - 'EA', - 'EB', - 'EE', - 'EH', - 'EK', - 'EL', - 'EM', - 'EP', - 'ER', - 'ES', - 'ET', - 'EW', - 'EX', - 'EY', - 'EZ', - 'GY', - 'HA', - 'HB', - 'HE', - 'HH', - 'HK', - 'HL', - 'HM', - 'HP', - 'HR', - 'HS', - 'HT', - 'HW', - 'HX', - 'HY', - 'HZ', - 'JA', - 'JB', - 'JC', - 'JE', - 'JG', - 'JH', - 'JJ', - 'JK', - 'JL', - 'JM', - 'JN', - 'JP', - 'JR', - 'JS', - 'JT', - 'JW', - 'JX', - 'JY', - 'JZ', - 'KA', - 'KB', - 'KE', - 'KH', - 'KK', - 'KL', - 'KM', - 'KP', - 'KR', - 'KS', - 'KT', - 'KW', - 'KX', - 'KY', - 'KZ', - 'LA', - 'LB', - 'LE', - 'LH', - 'LK', - 'LL', - 'LM', - 'LP', - 'LR', - 'LS', - 'LT', - 'LW', - 'LX', - 'LY', - 'LZ', - 'MA', - 'MW', - 'MX', - 'NA', - 'NB', - 'NE', - 'NH', - 'NL', - 'NM', - 'NP', - 'NR', - 'NS', - 'NW', - 'NX', - 'NY', - 'NZ', - 'OA', - 'OB', - 'OE', - 'OH', - 'OK', - 'OL', - 'OM', - 'OP', - 'OR', - 'OS', - 'OX', - 'PA', - 'PB', - 'PC', - 'PE', - 'PG', - 'PH', - 'PJ', - 'PK', - 'PL', - 'PM', - 'PN', - 'PP', - 'PR', - 'PS', - 'PT', - 'PW', - 'PX', - 'PY', - 'RA', - 'RB', - 'RE', - 'RH', - 'RK', - 'RM', - 'RP', - 'RR', - 'RS', - 'RT', - 'RW', - 'RX', - 'RY', - 'RZ', - 'SA', - 'SB', - 'SC', - 'SE', - 'SG', - 'SH', - 'SJ', - 'SK', - 'SL', - 'SM', - 'SN', - 'SP', - 'SR', - 'SS', - 'ST', - 'SW', - 'SX', - 'SY', - 'SZ', - 'TA', - 'TB', - 'TE', - 'TH', - 'TK', - 'TL', - 'TM', - 'TP', - 'TR', - 'TS', - 'TT', - 'TW', - 'TX', - 'TY', - 'TZ', - 'WA', - 'WB', - 'WE', - 'WK', - 'WL', - 'WM', - 'WP', - 'YA', - 'YB', - 'YE', - 'YH', - 'YK', - 'YL', - 'YM', - 'YP', - 'YR', - 'YS', - 'YT', - 'YW', - 'YX', - 'YY', - 'YZ', - 'ZA', - 'ZB', - 'ZE', - 'ZH', - 'ZK', - 'ZL', - 'ZM', - 'ZP', - 'ZR', - 'ZS', - 'ZT', - 'ZW', - 'ZX', - 'ZY', -]); - -export default PREFIXES; From 615d5295d0c13d07fa6f53c7cbc76f3d870ec3c8 Mon Sep 17 00:00:00 2001 From: Alfonso Strotgen Date: Tue, 20 Aug 2024 09:18:46 -0600 Subject: [PATCH 3/4] Follow the new prefix rules. --- src/gb/nino.ts | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/gb/nino.ts b/src/gb/nino.ts index e2f5a84f..584a5976 100644 --- a/src/gb/nino.ts +++ b/src/gb/nino.ts @@ -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 @@ -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 { return strings.cleanUnicode(input, ' -.'); @@ -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 = { From d3ffe823f24b7eb47e8aea61ed0c51611445a5ff Mon Sep 17 00:00:00 2001 From: Alfonso Strotgen Date: Tue, 20 Aug 2024 09:23:40 -0600 Subject: [PATCH 4/4] Update test. --- src/gb/nino.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/gb/nino.spec.ts b/src/gb/nino.spec.ts index 28a8fcf7..39f82205 100644 --- a/src/gb/nino.spec.ts +++ b/src/gb/nino.spec.ts @@ -85,9 +85,9 @@ describe('gb/nino', () => { expect(result.error).toBeInstanceOf(InvalidFormat); }); - it('validate:HO012345E', () => { - // Valid prefix, valid numeric section, invalid suffix - const result = validate('HH012345E'); + it('validate:TO012345D', () => { + // Invalid prefix, valid numeric section, valid suffix + const result = validate('TO012345D'); expect(result.error).toBeInstanceOf(InvalidFormat); });