|
| 1 | +# nin.py - functions for handling United Kingdom National Insurance Numbers |
| 2 | + |
| 3 | +"""NIN (United Kingdom National Insurance Number). |
| 4 | +
|
| 5 | +The National Insurance Number (NIN or NINO) is used in the United Kingdom to |
| 6 | +identify individuals for the social security system. It consists of two letters, |
| 7 | +six digits, and a final letter (suffix). |
| 8 | +
|
| 9 | +The format has several restrictions: |
| 10 | +- First letter cannot be D, F, I, Q, U, or V |
| 11 | +- Second letter cannot be D, F, I, Q, U, V, or O |
| 12 | +- Certain prefix combinations are invalid: BG, GB, NK, KN, NT, TN, ZZ |
| 13 | +- The suffix must be A, B, C, or D |
| 14 | +
|
| 15 | +More information: |
| 16 | +
|
| 17 | +* https://en.wikipedia.org/wiki/National_Insurance_number |
| 18 | +* https://www.gov.uk/hmrc-internal-manuals/national-insurance-manual/nim39110 |
| 19 | +
|
| 20 | +>>> validate('AB123456C') |
| 21 | +'AB123456C' |
| 22 | +>>> validate('ab 12 34 56 c') |
| 23 | +'AB123456C' |
| 24 | +>>> validate('DQ123456C') |
| 25 | +Traceback (most recent call last): |
| 26 | + ... |
| 27 | +stdnum.exceptions.InvalidFormat: First letter cannot be D, F, I, Q, U, or V |
| 28 | +>>> validate('BG123456A') # invalid prefix |
| 29 | +Traceback (most recent call last): |
| 30 | + ... |
| 31 | +stdnum.exceptions.InvalidFormat: Invalid prefix combination |
| 32 | +>>> validate('DF123456A') # invalid first two letters |
| 33 | +Traceback (most recent call last): |
| 34 | + ... |
| 35 | +stdnum.exceptions.InvalidFormat: First letter cannot be D, F, I, Q, U, or V |
| 36 | +>>> format('AB123456C') |
| 37 | +'AB 12 34 56 C' |
| 38 | +""" |
| 39 | + |
| 40 | +from stdnum.exceptions import * |
| 41 | +from stdnum.util import clean, isdigits |
| 42 | + |
| 43 | + |
| 44 | +def compact(number): |
| 45 | + """Convert the number to the minimal representation. This strips the |
| 46 | + number of any valid separators and removes surrounding whitespace.""" |
| 47 | + return clean(number, ' -').strip().upper() |
| 48 | + |
| 49 | + |
| 50 | +def validate(number): |
| 51 | + """Check if the number is valid. This checks the format against |
| 52 | + the official specifications.""" |
| 53 | + number = compact(number) |
| 54 | + |
| 55 | + # Basic length check |
| 56 | + if len(number) != 9: |
| 57 | + raise InvalidLength() |
| 58 | + |
| 59 | + # Check first letter restrictions |
| 60 | + if number[0] in 'DFIQUV': |
| 61 | + raise InvalidFormat('First letter cannot be D, F, I, Q, U, or V') |
| 62 | + |
| 63 | + # Check second letter restrictions |
| 64 | + if number[1] in 'DFIQUVO': |
| 65 | + raise InvalidFormat('Second letter cannot be D, F, I, Q, U, V, or O') |
| 66 | + |
| 67 | + # Check for invalid prefix combinations |
| 68 | + invalid_prefixes = {'BG', 'GB', 'NK', 'KN', 'NT', 'TN', 'ZZ'} |
| 69 | + if number[:2] in invalid_prefixes: |
| 70 | + raise InvalidFormat('Invalid prefix combination') |
| 71 | + |
| 72 | + # Check if middle 6 characters are digits |
| 73 | + if not isdigits(number[2:8]): |
| 74 | + raise InvalidFormat('Middle 6 characters must be digits') |
| 75 | + |
| 76 | + # Check if suffix is valid |
| 77 | + if number[8] not in 'ABCD': |
| 78 | + raise InvalidFormat('Suffix must be A, B, C, or D') |
| 79 | + |
| 80 | + return number |
| 81 | + |
| 82 | + |
| 83 | +def is_valid(number): |
| 84 | + """Check if the number is valid.""" |
| 85 | + try: |
| 86 | + return bool(validate(number)) |
| 87 | + except ValidationError: |
| 88 | + return False |
| 89 | + |
| 90 | + |
| 91 | +def format(number, separator=' '): |
| 92 | + """Reformat the number to the standard presentation format.""" |
| 93 | + number = compact(number) |
| 94 | + return separator.join([number[0:2], number[2:4], number[4:6], number[6:8], number[8:]]) |
0 commit comments