Skip to content

Commit a457382

Browse files
authored
fix: uae personal tin validation updated (#44)
1 parent fcd4dd1 commit a457382

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

stdnum/ae/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from stdnum.ae import trn as vat # noqa: F401
22
from stdnum.ae import trn as business_tin # noqa: F401
3-
from stdnum.ae import trn as personal_tin # noqa: F401
3+
from stdnum.ae import eid as personal_tin # noqa: F401

stdnum/ae/eid.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# eid.py - functions for handling UAE Emirates ID numbers
2+
# ref:
3+
# https://www.icp.gov.ae/en/
4+
5+
"""Emirates ID (Emirates Identity Card Number)
6+
7+
The Emirates ID is a 15 digit number.
8+
9+
>>> validate('123456789012345')
10+
'123456789012345'
11+
>>> validate('1234-5678901-2345')
12+
'123456789012345'
13+
>>> validate('1234567890123456')
14+
Traceback (most recent call last):
15+
...
16+
stdnum.exceptions.InvalidLength: The number has an invalid length.
17+
>>> validate(None)
18+
Traceback (most recent call last):
19+
...
20+
stdnum.exceptions.InvalidFormat: The number has an invalid format.
21+
"""
22+
23+
import re
24+
25+
from stdnum.exceptions import *
26+
from stdnum.util import clean
27+
28+
29+
_eid_re = re.compile(r'^\d{15}$')
30+
31+
32+
def compact(number):
33+
"""Convert the number to the minimal representation. This strips the
34+
number of any valid separators and removes surrounding whitespace."""
35+
return clean(number, ' -').strip()
36+
37+
38+
def validate(number):
39+
"""Check if the number is a valid Emirates ID. This checks the length and format."""
40+
number = compact(number)
41+
if len(number) != 15:
42+
raise InvalidLength()
43+
if not _eid_re.match(number):
44+
raise InvalidFormat()
45+
return number
46+
47+
48+
def is_valid(number):
49+
"""Check if the number is a valid Emirates ID."""
50+
try:
51+
return bool(validate(number))
52+
except ValidationError:
53+
return False

tests/test_ae_eid.doctest

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
>>> from stdnum.ae import eid
2+
3+
>>> eid.validate('123456789012345')
4+
'123456789012345'
5+
>>> eid.is_valid('123456789012345')
6+
True
7+
>>> eid.validate('1234-5678901-2345')
8+
'123456789012345'
9+
>>> eid.is_valid('1234-5678901-2345')
10+
True
11+
>>> eid.validate(' 1234-5678901-2345 ')
12+
'123456789012345'
13+
>>> eid.is_valid(' 1234-5678901-2345 ')
14+
True
15+
>>> eid.validate(' 1234-5678901-2 345 ')
16+
'123456789012345'
17+
>>> eid.is_valid(' 1234-5678901-2 345 ')
18+
True
19+
>>> eid.validate('1234567890123456')
20+
Traceback (most recent call last):
21+
...
22+
stdnum.exceptions.InvalidLength: The number has an invalid length.
23+
>>> eid.is_valid('1234567890123456')
24+
False
25+
>>> eid.validate('12345678901234A')
26+
Traceback (most recent call last):
27+
...
28+
stdnum.exceptions.InvalidFormat: The number has an invalid format.
29+
>>> eid.is_valid('1234-567890123456')
30+
False

0 commit comments

Comments
 (0)