Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion stdnum/lei.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

from stdnum.exceptions import *
from stdnum.iso7064 import mod_97_10
from stdnum.util import clean
from stdnum.util import clean, isdigits


def compact(number: str) -> str:
Expand All @@ -55,6 +55,12 @@ def validate(number: str) -> str:
"""Check if the number is valid. This checks the length, format and check
digits."""
number = compact(number)
if not all(x in '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' for x in number):
raise InvalidFormat()
if len(number) != 20:
raise InvalidLength()
if not isdigits(number[-2:]):
raise InvalidFormat()
mod_97_10.validate(number)
return number

Expand Down
23 changes: 23 additions & 0 deletions tests/test_lei.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ This file contains more detailed doctests for the stdnum.lei module.
>>> from stdnum.exceptions import *


The number should be exactly 20 characters long. Leading zeros change the
length but not the checksum.

>>> lei.validate('213800KUD8LAJWSQ9D15')
'213800KUD8LAJWSQ9D15'
>>> lei.validate('0213800KUD8LAJWSQ9D15')
Traceback (most recent call last):
...
InvalidLength: ...
>>> lei.validate('1')
Traceback (most recent call last):
...
InvalidLength: ...


The last two characters are the check digits and should be numeric.

>>> lei.validate('213800KUD8LAJWSQ9DAD')
Traceback (most recent call last):
...
InvalidFormat: ...


These have been found online and should all be valid numbers.

>>> numbers = '''
Expand Down