Functional Weave
Code in Python

validation.uk-ni-number

Check a UK National Insurance number's format and HMRC's disallowed prefix letters, and normalise it.

2.0.0 · published 2026-10-03 by charlie · Anterra

Pinned by 22 tests, run in TypeScript, Python and Rust.

What it does

Checks that a UK National Insurance number (NINO) has the shape HMRC issues and uses none of the prefix letters HMRC rules out, and returns it upper case with the spaces removed, the form payroll submissions (RTI) carry.

A NINO HAS NO CHECK DIGIT. A pass means the number could have been issued; only HMRC or DWP can say it was, and to whom. Treat this as typo-catching at the keyboard.

For example

  • validate_uk_ni_number(AB123456C) → valid true, normalised AB123456C, reason — a well-formed number
  • validate_uk_ni_number(ab 12 34 56 c) → valid true, normalised AB123456C, reason — lower case and spaced as printed on a letter
  • validate_uk_ni_number( JG103759A ) → valid true, normalised JG103759A, reason — stray leading and trailing spaces

The function

The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.

def validate_uk_ni_number(value: str) -> UkNiNumber
valuestringa NINO in any case, optionally spaced: "QQ 12 34 56 A"
returnsUkNiNumber

The type it declares, generated into your project

@dataclass(frozen=True)
class UkNiNumber:
    """normalised is null when valid is false."""

    valid: bool
    #: nine characters, upper case, no spaces: "AB123456C"
    normalised: Optional[str]
    #: null when valid; empty, bad-character, bad-length, bad-format, bad-prefix or bad-suffix
    reason: Optional[str]

Your code names it in one line, in the file that uses it

from fune.validation.uk_ni_number import validate_uk_ni_number  # validation.uk-ni-number@^2
impl/python.py · 62 lines · open · raw
from .validation_uk_ni_number_types import UkNiNumber

#: HMRC NIM39110: D, F, I, Q, U and V are never either letter of a prefix,
#: and O is never the second.
_BARRED_FIRST = "DFIQUV"
_BARRED_SECOND = "DFIOQUV"

#: Prefixes made of allowed letters that HMRC still never issues.
_BARRED_PREFIXES = ("BG", "GB", "KN", "NK", "NT", "TN", "ZZ")

#: The suffix records the quarter a contribution card was once returned in,
#: and is always one of these four.
_SUFFIXES = "ABCD"


def _invalid(reason: str) -> UkNiNumber:
    return UkNiNumber(valid=False, normalised=None, reason=reason)


def _is_letter(ch: str) -> bool:
    return "A" <= ch <= "Z"


def _is_digit(ch: str) -> bool:
    return "0" <= ch <= "9"


def validate_uk_ni_number(value: str) -> UkNiNumber:
    """Check a National Insurance number's shape and prefix rules.

    There is no check digit in a NINO, so a pass means "could have been
    issued", never "belongs to this person". The checks run in a fixed order
    (characters, length, shape, prefix, suffix) so that every language reports
    the same first reason.
    """
    if not isinstance(value, str):
        return _invalid("empty")
    compact = ""
    for ch in value:
        # Only the ASCII space is ignored: NINOs are printed in pairs.
        if ch == " ":
            continue
        # ASCII-only upper-casing, so no language's Unicode rules can turn a
        # stray character into a letter that passes.
        if "a" <= ch <= "z":
            ch = chr(ord(ch) - 32)
        if not _is_letter(ch) and not _is_digit(ch):
            return _invalid("bad-character")
        compact += ch
    if compact == "":
        return _invalid("empty")
    if len(compact) != 9:
        return _invalid("bad-length")
    if not (_is_letter(compact[0]) and _is_letter(compact[1]) and _is_letter(compact[8])):
        return _invalid("bad-format")
    if not all(_is_digit(ch) for ch in compact[2:8]):
        return _invalid("bad-format")
    if compact[0] in _BARRED_FIRST or compact[1] in _BARRED_SECOND or compact[0:2] in _BARRED_PREFIXES:
        return _invalid("bad-prefix")
    if compact[8] not in _SUFFIXES:
        return _invalid("bad-suffix")
    return UkNiNumber(valid=True, normalised=compact, reason=None)

Install

fune build

With that line in your source, in a Python project (language python in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:

fune add validation.uk-ni-number
Download for Python validation.uk-ni-number-2.0.0-python.fune · 10,595 bytes sha256 e4ab2ca40d21ae4eaa7ea95478ded39d28f001f590dc9a27eb08e4c0d43a7d90

The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./validation.uk-ni-number-2.0.0-python.fune, or fetch it from a terminal with fune pull validation.uk-ni-number@2.0.0:python.

The whole function, every language, is one file too: validation.uk-ni-number-2.0.0.fune, 16,329 bytes, sha256 64cd5b29cb5b59b7db384f18e0d7d3705f1b70d3e382266a9a09b96170171293. It installs into a project of any language.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

# fune: before validation.uk-ni-number

after — your function gets the result and the arguments, and returns the final result.

# fune: after validation.uk-ni-number

replace — it requires no other capability, so there is no dependency to replace.

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show validation.uk-ni-number --steps.

# fune: step validation.uk-ni-number after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.

CaseArgumentsExpected
a well-formed number AB123456C → valid true, normalised AB123456C, reason —
lower case and spaced as printed on a letter ab 12 34 56 c → valid true, normalised AB123456C, reason —
stray leading and trailing spaces JG103759A → valid true, normalised JG103759A, reason —
each suffix A to D is allowed; D here CE000001D → valid true, normalised CE000001D, reason —
O may start a prefix: it is only barred as the second letter OA123456B → valid true, normalised OA123456B, reason —
HMRC's own example QQ 12 34 56 A is deliberately not a valid number QQ 12 34 56 A → valid false, normalised —, reason bad-prefix
D is never a first letter DA123456A → valid false, normalised —, reason bad-prefix
V is never a second letter AV123456A → valid false, normalised —, reason bad-prefix
O is never a second letter, so the OO administrative prefix fails OO123456A → valid false, normalised —, reason bad-prefix
GB is a barred prefix although both letters are allowed GB123456A → valid false, normalised —, reason bad-prefix
Show the other 12 tests
CaseArgumentsExpected
TN, the retired temporary-number prefix, is barred TN123456A → valid false, normalised —, reason bad-prefix
ZZ is barred ZZ123456A → valid false, normalised —, reason bad-prefix
NK is barred NK123456A → valid false, normalised —, reason bad-prefix
E is not a suffix AB123456E → valid false, normalised —, reason bad-suffix
the old PP999999P pension reporting reference fails on its suffix PP999999P → valid false, normalised —, reason bad-suffix
a missing suffix is too short AB123456 → valid false, normalised —, reason bad-length
a digit where a prefix letter belongs A1123456C → valid false, normalised —, reason bad-format
a letter among the six digits AB12345CD → valid false, normalised —, reason bad-format
hyphens are not a NINO separator AB-12-34-56-C → valid false, normalised —, reason bad-character
a non-ASCII letter ÅB123456C → valid false, normalised —, reason bad-character
the empty string → valid false, normalised —, reason empty
only spaces → valid false, normalised —, reason empty

More from the author

## The rules (HMRC NIM39110)

- Two prefix letters, six digits, one suffix letter. - D, F, I, Q, U and V are never used as either prefix letter. - O is never the second prefix letter. It may be the first: "OA123456B" passes, and a validator that bans O everywhere gets this wrong. - BG, GB, KN, NK, NT, TN and ZZ are never used as prefixes. - The suffix is always A, B, C or D. Some older systems accept a blank suffix; HMRC's manual says the suffix is always present, so it is required.

HMRC's printed example, "QQ 12 34 56 A", is deliberately invalid (Q is barred), and is a vector here.

Administrative references that look like NINOs are not rejected by name. OO (tax credits) and TN (retired temporary numbers) already fail the letter rules and PP999999P fails on its suffix. FY, NC, PY and PZ are administrative prefixes the manual describes as out of use, but it does not list them as barred, so they pass. Temporary Reference Numbers ("11 a1 11 11") are not NINOs and fail.

## Input and result

Case is ignored and ASCII spaces are ignored anywhere. Anything else that is not an ASCII letter or digit (hyphens, dots, non-breaking spaces, accented letters) is `bad-character`. Validators answer rather than throw.

| reason | meaning (checked in this order) | |---|---| | `empty` | nothing but spaces, or not a string | | `bad-character` | a character other than an ASCII letter, digit or space | | `bad-length` | not nine characters once spaces are removed | | `bad-format` | not two letters, six digits and a letter | | `bad-prefix` | a barred letter or prefix | | `bad-suffix` | the last letter is not A, B, C or D |

## Source

HMRC National Insurance Manual, NIM39110 "National Insurance Numbers (NINOs): Format and Security: What a NINO looks like", read 22 September 2026: https://www.gov.uk/hmrc-internal-manuals/national-insurance-manual/nim39110

2.0.0 renames the function from `ukNiNumber` to `validateUkNiNumber` (`validate_uk_ni_number` in Python and Rust), so every validator that returns a result record is `validateX` and every one that returns a bool is `isX`. Nothing else changed; 1.0.0 stays published under the old name.

Files

PathBytes
README.md2,572
impl/python.py2,251
impl/rust.rs3,158
impl/typescript.ts2,292
vectors.json3,279