diff options
author | Dimitri Papadopoulos <dimitri.papadopoulos@cea.fr> | 2016-05-29 17:57:05 +0200 |
---|---|---|
committer | Arthur de Jong <arthur@arthurdejong.org> | 2016-07-26 13:08:14 +0200 |
commit | 879f2d3c8a777b4ae8a1b97fdd0629e90d7bc871 (patch) | |
tree | 591b7ebcdde54635bd8e77a01a215d24af650e61 | |
parent | fd9f9538c362aacf50e4ae32ae51b15ecaf79184 (diff) |
Improve French NIR validation
Please note that the 7th character of the NIR might be 'A' or 'B'. Other
than that the NIR contains digits only.
-rw-r--r-- | stdnum/fr/nir.py | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/stdnum/fr/nir.py b/stdnum/fr/nir.py index bd9ce80..d8c26ee 100644 --- a/stdnum/fr/nir.py +++ b/stdnum/fr/nir.py @@ -48,25 +48,34 @@ from stdnum.util import clean def compact(number): """Convert the number to the minimal representation. This strips the number of any valid separators and removes surrounding whitespace.""" - return clean(number, ' .').strip() + return clean(number, ' .').strip().upper() def validate(number): """Checks to see if the number provided is valid. This checks the length - and check digit.""" + and check digits.""" number = compact(number) - if not number.isdigit(): + if not (number.isdigit() or ( + number[:5].isdigit() and number[7:].isdigit() and + number[5:7] in ('2A', '2B'))): raise InvalidFormat() if len(number) != 15: raise InvalidLength() - if (97 - (int(number[:13]) % 97)) != int(number[13:]): + department = number[5:7] + if department == '2A': + s = number[:5] + '19' + number[7:13] + elif department == '2B': + s = number[:5] + '18' + number[7:13] + else: + s = number[:13] + if (97 - (int(s) % 97)) != int(number[13:]): raise InvalidChecksum() return number def is_valid(number): """Checks to see if the number provided is valid. This checks the length - and check digit.""" + and check digits.""" try: return bool(validate(number)) except ValidationError: |