Skip to content

Commit 3e55c5b

Browse files
committed
Add Kazakhstan Business Identification Number validation
Prevent future date output If the date returned is of the current year, it could be in the future, and thus invalidate the number. Update IIN description
1 parent 94b10d2 commit 3e55c5b

4 files changed

Lines changed: 21 additions & 15 deletions

File tree

‎stdnum/kz/__init__.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
# Lesser General Public License for more details.
1515
#
1616
# You should have received a copy of the GNU Lesser General Public
17-
# License along with this library; if not, write to the Free Software
18-
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19-
# 02110-1301 USA
17+
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
2018

2119
"""Collection of Kazakhstan numbers."""
2220

‎stdnum/kz/bin.py‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
# Lesser General Public License for more details.
1515
#
1616
# You should have received a copy of the GNU Lesser General Public
17-
# License along with this library; if not, write to the Free Software
18-
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19-
# 02110-1301 USA
17+
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
2018

2119
"""BIN or "БСН" (Business Identification Number, "бизнес-сәйкестендіру нөмірі",
2220
Kazakhstan tax number).
@@ -66,15 +64,20 @@
6664
Traceback (most recent call last):
6765
...
6866
InvalidComponent: ...
67+
>>> validate('940140000386')
68+
Traceback (most recent call last):
69+
...
70+
InvalidChecksum: ...
6971
>>> format('990 140 004 654')
7072
'990140004654'
7173
""" # noqa: E501
7274

7375
from stdnum.exceptions import *
76+
from stdnum.kz.iin import calc_check_digit
7477
from stdnum.util import clean, isdigits
7578

7679

77-
def compact(number):
80+
def compact(number: str) -> str:
7881
"""Convert the number to the minimal representation.
7982
8083
This strips the number of any valid separators and removes surrounding
@@ -83,7 +86,7 @@ def compact(number):
8386
return clean(number, ' ')
8487

8588

86-
def validate(number):
89+
def validate(number: str) -> str:
8790
"""Check if the number is a valid Kazakhstan BIN number.
8891
8992
This checks the length and formatting.
@@ -99,17 +102,19 @@ def validate(number):
99102
raise InvalidComponent()
100103
if number[5] not in ('0', '1', '2', '3'):
101104
raise InvalidComponent()
105+
if calc_check_digit(number[:-1]) != number[-1]:
106+
raise InvalidChecksum()
102107
return number
103108

104109

105-
def is_valid(number):
110+
def is_valid(number: str) -> bool:
106111
"""Check if the number is a valid Kazakhstan BIN number."""
107112
try:
108113
return bool(validate(number))
109114
except ValidationError:
110115
return False
111116

112117

113-
def format(number):
118+
def format(number: str) -> str:
114119
"""Reformat the number to the standard presentation format."""
115120
return compact(number)

‎stdnum/kz/iin.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"""IIN (ЖСН, Жеке сәйкестендіру нөмірі, Kazakhstani Individual identification number).
2020
2121
It is a 12-digit number of which the first 6 digits denote the person's
22-
birth date, the next three digits represent a birth order number
22+
birth date in the format YYMMDD, the next digit encodes the gender and
23+
century of birth, the next four digits represent a birth serial number
2324
and the last digit is a check digit.
2425
2526
* https://www.gov.kz/article/648?lang=kk
@@ -83,7 +84,7 @@ def get_birth_date(number: str) -> datetime.date:
8384
if number[6] == '0': # no century info for foreign nationals
8485
today = datetime.date.today()
8586
year += (today.year // 100) * 100
86-
if year > today.year:
87+
if year >= today.year:
8788
year -= 100
8889

8990
elif number[6] in '12':

‎tests/test_kz_bin.doctest‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1313
Lesser General Public License for more details.
1414

1515
You should have received a copy of the GNU Lesser General Public
16-
License along with this library; if not, write to the Free Software
17-
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18-
02110-1301 USA
16+
License along with this library; if not, see <https://www.gnu.org/licenses/>.
1917

2018

2119
This file contains more detailed doctests for the stdnum.kz.bin module. It
@@ -53,6 +51,10 @@ InvalidComponent: ...
5351
Traceback (most recent call last):
5452
...
5553
InvalidComponent: ...
54+
>>> bin.validate('940140000386')
55+
Traceback (most recent call last):
56+
...
57+
InvalidChecksum: ...
5658

5759

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

0 commit comments

Comments
 (0)