Functional Weave
Code in TypeScript

validation.lei@1.0.0

README.md

2,308 bytes · view raw

# validation.lei

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.

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).