Functional Weave
Code in TypeScript

validation.lei

Check a 20-character Legal Entity Identifier against its ISO 17442 / ISO 7064 MOD 97-10 check digits.

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

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

What it does

Checks a Legal Entity Identifier, the 20-character code every counterparty in a regulated trade reports under EMIR and MiFID II. ISO 17442 fixes the shape:

- characters 1-4: the prefix of the Local Operating Unit (LOU) that issued it, returned as `louPrefix`; - characters 5-18: letters and digits chosen by the LOU; - characters 19-20: two check digits.

For example

  • validateLei(506700GE1G29325QX363) → valid true, normalised 506700GE1G29325QX363, lou prefix 5067, reason — GLEIF's own LEI
  • validateLei(HWUPKR0MPOU8FGXBT394) → valid true, normalised HWUPKR0MPOU8FGXBT394, lou prefix HWUP, reason — Apple Inc.'s published LEI
  • validateLei(7LTWFZYICNSX8D621K86) → valid true, normalised 7LTWFZYICNSX8D621K86, lou prefix 7LTW, reason — Deutsche Bank's published LEI

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.

export function validateLei(value: string): LeiCheck
valuestringa 20-character LEI in any case, optionally spaced
returnsLeiCheck

The type it declares, generated into your project

/** The result of checking an LEI. Every string is null when valid is false; reason is null when it is true. */
export interface LeiCheck {
  readonly valid: boolean;
  /** upper case, no spaces, 20 characters */
  readonly normalised: string | null;
  /** the first four characters, which identify the issuing organisation (LOU) */
  readonly louPrefix: string | null;
  /** empty, bad-character, bad-length, bad-format or bad-check-digit */
  readonly reason: string | null;
}

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

import { validateLei } from "#fune/validation.lei@^1";
impl/typescript.ts · 53 lines · open · raw
import { type LeiCheck } from "./validation_lei_types.ts";

function invalid(reason: string): LeiCheck {
  return { valid: false, normalised: null, louPrefix: null, reason };
}

function isDigit(ch: string): boolean {
  return ch >= "0" && ch <= "9";
}

/**
 * ISO 7064 MOD 97-10 over the whole LEI, letters read as A = 10 ... Z = 35.
 *
 * The expansion is up to 40 digits, past any 64-bit integer, so the remainder
 * is carried one character at a time. A letter contributes two digits and so
 * multiplies the running remainder by 100; the largest intermediate is
 * 96 * 100 + 35.
 */
function mod97(lei: string): number {
  let remainder = 0;
  for (let i = 0; i < lei.length; i++) {
    const code = lei.charCodeAt(i);
    remainder = code <= 57 ? (remainder * 10 + (code - 48)) % 97 : (remainder * 100 + (code - 55)) % 97;
  }
  return remainder;
}

/**
 * Check a Legal Entity Identifier's format and ISO 17442 check digits.
 *
 * Well formed is not issued: only GLEIF says whether an LEI is current.
 * Validators answer rather than throw.
 */
export function validateLei(value: string): LeiCheck {
  if (typeof value !== "string") return invalid("empty");

  let lei = "";
  for (const ch of value) {
    if (ch === " ") continue;
    // Folded by hand: toUpperCase() is Unicode-aware and Rust's ASCII fold is not.
    lei += ch >= "a" && ch <= "z" ? String.fromCharCode(ch.charCodeAt(0) - 32) : ch;
  }
  if (lei.length === 0) return invalid("empty");
  for (const ch of lei) {
    if (!isDigit(ch) && !(ch >= "A" && ch <= "Z")) return invalid("bad-character");
  }
  if (lei.length !== 20) return invalid("bad-length");
  if (!isDigit(lei[18]) || !isDigit(lei[19])) return invalid("bad-format");
  // The remainder must be 1, not 0: the check digits are chosen so that it is.
  if (mod97(lei) !== 1) return invalid("bad-check-digit");

  return { valid: true, normalised: lei, louPrefix: lei.slice(0, 4), reason: null };
}

Install

fune build

With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the TypeScript 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.lei
Download for TypeScript validation.lei-1.0.0-typescript.fune · 9,272 bytes sha256 3af358b46891e4b5fe8edfd057848e00c353654ebd4240138dc183f48539e0b6

The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./validation.lei-1.0.0-typescript.fune, or fetch it from a terminal with fune pull validation.lei@1.0.0:typescript.

The whole function, every language, is one file too: validation.lei-1.0.0.fune, 13,996 bytes, sha256 fde9af0883131cea8a51a63bf4bfb2b0bf9f39bd4aca36513ebfebb722d46a54. 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.lei

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

// fune: after validation.lei

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.lei --steps.

// fune: step validation.lei 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
GLEIF's own LEI 506700GE1G29325QX363 → valid true, normalised 506700GE1G29325QX363, lou prefix 5067, reason —
Apple Inc.'s published LEI HWUPKR0MPOU8FGXBT394 → valid true, normalised HWUPKR0MPOU8FGXBT394, lou prefix HWUP, reason —
Deutsche Bank's published LEI 7LTWFZYICNSX8D621K86 → valid true, normalised 7LTWFZYICNSX8D621K86, lou prefix 7LTW, reason —
a published LEI from the 5493 LOU 5493001KJTIIGC8Y1R12 → valid true, normalised 5493001KJTIIGC8Y1R12, lou prefix 5493, reason —
lower case and spaces are normalised 5067 00ge 1g29 325q x363 → valid true, normalised 506700GE1G29325QX363, lou prefix 5067, reason —
the last check digit off by one 506700GE1G29325QX364 → valid false, normalised —, lou prefix —, reason bad-check-digit
two adjacent characters transposed 506700EG1G29325QX363 → valid false, normalised —, lou prefix —, reason bad-check-digit
a single character changed in the body HWUPKR0MPOU8FGXBT494 → valid false, normalised —, lou prefix —, reason bad-check-digit
a letter among the check digits is a bad format 506700GE1G29325QX3A3 → valid false, normalised —, lou prefix —, reason bad-format
nineteen characters is too short 506700GE1G29325QX36 → valid false, normalised —, lou prefix —, reason bad-length
Show the other 4 tests
CaseArgumentsExpected
twenty-one characters is too long 506700GE1G29325QX3630 → valid false, normalised —, lou prefix —, reason bad-length
a hyphen is a bad character, reported before the length 506700-GE1G29325QX363 → valid false, normalised —, lou prefix —, reason bad-character
the empty string → valid false, normalised —, lou prefix —, reason empty
a non-string is empty, not an exception — → valid false, normalised —, lou prefix —, reason empty

More from the author

THE CHECK is ISO 7064 MOD 97-10, the same arithmetic as an IBAN's: read each letter as a number (A = 10 ... Z = 35), keep digits as they are, and the whole 20-character number must leave a remainder of 1 when divided by 97. A remainder of 0 is wrong, which is the mistake a validator written from memory makes. The expanded number is up to 40 digits long, too long for a 64-bit integer, so all three implementations carry the remainder along one character at a time.

This is the same arithmetic `validation.iban` uses, but that capability keeps its mod-97 routine private (nothing to import), and a level-0 capability cannot require another, so the dozen lines are written out here.

A PASSING LEI IS WELL FORMED, NOT NECESSARILY ISSUED OR CURRENT. LEIs lapse if not renewed each year; only the GLEIF database (https://search.gleif.org) says whether one is issued, lapsed or retired, and whose it is.

ACCEPTED INPUT: letters in either case (folded to upper case) and digits, with ASCII spaces ignored anywhere ("5493 001K ..."). Anything else is refused.

| reason | meaning | |-------------------|----------------------------------------------------------| | `empty` | nothing but spaces, or not a string at all | | `bad-character` | anything other than A-Z, a-z, 0-9 and spaces | | `bad-length` | not exactly 20 characters | | `bad-format` | the last two characters are not both digits | | `bad-check-digit` | the MOD 97-10 remainder is not 1 |

The checks run in that order; the first failure is the reason. Validators answer rather than throw.

Sources: ISO 17442-1:2020 "Financial services - Legal entity identifier (LEI)"; ISO/IEC 7064:2003 MOD 97-10; GLEIF, "LEI in detail" (https://www.gleif.org/en/about-lei/iso-17442-the-lei-code-structure).

Files

PathBytes
README.md2,308
impl/python.py1,880
impl/rust.rs2,629
impl/typescript.ts1,935
vectors.json2,527