Skip to content

Commit d332ee1

Browse files
committed
Add check digit validation to Senegal NINEA
1 parent dd22123 commit d332ee1

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

stdnum/sn/ninea.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# coding: utf-8
33
#
44
# Copyright (C) 2023 Leandro Regueiro
5+
# Copyright (C) 2026 Arthur de Jong
56
#
67
# This library is free software; you can redistribute it and/or
78
# modify it under the terms of the GNU Lesser General Public
@@ -41,6 +42,10 @@
4142
'30672212G2'
4243
>>> validate('3067221 2G2')
4344
'30672212G2'
45+
>>> validate('3067222')
46+
Traceback (most recent call last):
47+
...
48+
InvalidChecksum: ...
4449
>>> validate('1234567 0AZ')
4550
Traceback (most recent call last):
4651
...
@@ -109,6 +114,12 @@ def _validate_cofi(number: str) -> None:
109114
raise InvalidComponent()
110115

111116

117+
def _checksum(number: str) -> int:
118+
number = number.zfill(9)
119+
weights = (1, 2, 1, 2, 1, 2, 1, 2, 1)
120+
return sum(int(n) * w for n, w in zip(number, weights)) % 10
121+
122+
112123
def validate(number: str) -> str:
113124
"""Check if the number is a valid Senegal NINEA.
114125
@@ -125,6 +136,8 @@ def validate(number: str) -> str:
125136
raise InvalidFormat()
126137
if cofi:
127138
_validate_cofi(cofi)
139+
if _checksum(number) != 0:
140+
raise InvalidChecksum()
128141
return number + cofi
129142

130143

tests/test_sn_ninea.doctest

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ These have been found online and should all be valid numbers.
8686
... 0020884 2 G 3
8787
... 002420983 2G3
8888
... 002502343
89-
... 00284430 C0
9089
... 004237633 2B2
9190
... 004343430
9291
... 0044440722V1
@@ -109,7 +108,6 @@ These have been found online and should all be valid numbers.
109108
... 005721809
110109
... 005754339 2V2
111110
... 005844700
112-
... 006,364,472 2L2
113111
... 00605 33 92
114112
... 006208434
115113
... 006269436
@@ -161,7 +159,6 @@ These have been found online and should all be valid numbers.
161159
... 21948852B9
162160
... 22486742 S 3
163161
... 24312110V9
164-
... 244982000
165162
... 25437852G3
166163
... 255 44 772 S 3
167164
... 25833512R2

0 commit comments

Comments
 (0)