Skip to content

Commit 05c35b0

Browse files
committed
Added finnish ssn validator
1 parent 75c716e commit 05c35b0

File tree

5 files changed

+82
-36
lines changed

5 files changed

+82
-36
lines changed

validators/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
from .between import between
22
from .email import email
33
from .extremes import Min, Max
4-
from .finnish_business_id import finnish_business_id
54
from .ip_address import ipv4, ipv6
65
from .length import length
76
from .mac_address import mac_address
87
from .truthy import truthy
98
from .url import url
109
from .utils import ValidationFailure, validator
1110
from .uuid import uuid
11+
from .i18n import finnish_business_id, finnish_ssn
1212

1313

1414
__all__ = (
1515
between,
1616
email,
1717
finnish_business_id,
18+
finnish_ssn,
1819
ipv4,
1920
ipv6,
2021
length,

validators/email.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
r"(\.[-!#$%&'*+/=?^_`{}|~0-9A-Z]+)*$"
99
# quoted-string
1010
r'|^"([\001-\010\013\014\016-\037!#-\[\]-\177]|'
11-
r'\\[\001-\011\013\014\016-\177])*"$)',
11+
r"""\\[\001-\011\013\014\016-\177])*"$)""",
1212
re.IGNORECASE
1313
)
1414
domain_regex = re.compile(

validators/finnish_business_id.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

validators/i18n/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from .fi import finnish_ssn, finnish_business_id
2+
3+
4+
__all__ = (
5+
finnish_ssn,
6+
finnish_business_id,
7+
)

validators/i18n/fi.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import re
2+
from validators.utils import validator
3+
4+
5+
business_id_pattern = re.compile(r'^[0-9]{7}-[0-9]$')
6+
ssn_checkmarks = '0123456789ABCDEFHJKLMNPRSTUVWXY'
7+
ssn_pattern = re.compile(
8+
r"""^
9+
(?P<date>([0-2]\d|3[01])
10+
(0\d|1[012])
11+
(\d{{2}}))
12+
[A+-]
13+
(?P<serial>(\d{{3}}))
14+
(?P<checksum>[{checkmarks}])$""".format(checkmarks=ssn_checkmarks),
15+
re.VERBOSE | re.IGNORECASE
16+
)
17+
18+
19+
@validator
20+
def finnish_business_id(business_id):
21+
"""
22+
Validates a Finnish Business ID. Each company in Finland has a distinct
23+
business id. For more information see `Finnish Trade Register`_
24+
25+
.. _Finnish Trade Register:
26+
http://en.wikipedia.org/wiki/Finnish_Trade_Register
27+
28+
::
29+
30+
>>> assert finnish_business_id('0112038-9') # Fast Monkeys Ltd
31+
32+
>>> assert not finnish_business_id('1234567-8') # Bogus ID
33+
34+
.. versionadded:: 0.4
35+
36+
:param business_id: business_id to validate
37+
"""
38+
if not business_id or not re.match(business_id_pattern, business_id):
39+
return False
40+
factors = [7, 9, 10, 5, 8, 4, 2]
41+
numbers = map(int, business_id[:7])
42+
checksum = int(business_id[8])
43+
sum_ = sum(f * n for f, n in zip(factors, numbers))
44+
modulo = sum_ % 11
45+
return (11 - modulo == checksum) or (modulo == 0 and checksum == 0)
46+
47+
48+
@validator
49+
def finnish_ssn(ssn):
50+
"""
51+
Validates a Finnish Social Security Number. This validator is based on
52+
`django-localflavor-fi`_.
53+
54+
.. _django-localflavor-fi:
55+
https://github.com/django/django-localflavor-fi/
56+
57+
.. versionadded:: 0.5
58+
59+
:param ssn: Social Security Number to validate
60+
"""
61+
if not ssn:
62+
return False
63+
64+
result = re.match(ssn_pattern, ssn)
65+
if not result:
66+
return False
67+
gd = result.groupdict()
68+
checksum = int(gd['date'] + gd['serial'])
69+
return (
70+
ssn_checkmarks[checksum % len(ssn_checkmarks)] ==
71+
gd['checksum'].upper()
72+
)

0 commit comments

Comments
 (0)