Skip to content

Check length and format of LEI numbers - #525

Open
raams95 wants to merge 1 commit into
arthurdejong:masterfrom
raams95:lei-check-length-and-format
Open

raams95 wants to merge 1 commit into
arthurdejong:masterfrom
raams95:lei-check-length-and-format

Conversation

@raams95

@raams95 raams95 commented Oct 24, 2026

Copy link
Copy Markdown

lei.validate() says it checks the length, format and check digits, but it only runs the ISO 7064 Mod 97, 10 check. So any string that happens to pass the checksum is accepted:

>>> from stdnum import lei
>>> lei.validate('1')
'1'
>>> lei.validate('0213800KUD8LAJWSQ9D15')   # valid LEI with a leading zero
'0213800KUD8LAJWSQ9D15'
>>> lei.validate('213800KUD8LAJWSQ9DAD')    # letters as check digits
'213800KUD8LAJWSQ9DAD'

An LEI (ISO 17442) is always 20 characters, letters and digits only, and the last two are the check digits. Mod 97, 10 always yields two decimal digits there. GLEIF describes the structure here: https://www.gleif.org/en/about-lei/iso-17442-the-lei-code-structure

This change makes validate() raise InvalidFormat for characters outside 0-9/A-Z, InvalidLength if the number isn't 20 characters, and InvalidFormat if the last two characters aren't digits. It doesn't touch compact() (the upper-casing discussed in #509 stays as it is). All the real LEIs in tests/test_lei.doctest still validate.

I added cases to tests/test_lei.doctest. They fail on master and pass with the change. The full pytest run passes with 100% coverage, and flake8 . is clean.

The validate() function only checked the ISO 7064 Mod 97, 10 checksum,
so any string that passed the checksum was accepted, e.g. "1", an LEI
with extra leading zeros or one with letters as check digits.

This ensures that the number is 20 characters long, only contains
letters and digits and ends with two numeric check digits.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant