Skip to content

Commit 96abcfe

Browse files
Victorarthurdejong
authored andcommitted
Add Spanish postcode validator
Closes #401
1 parent 36858cc commit 96abcfe

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

stdnum/es/postal_code.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# postal_code.py - functions for handling Spanish postal code numbers
2+
# coding: utf-8
3+
#
4+
# Copyright (C) 2023 Víctor Ramos
5+
#
6+
# This library is free software; you can redistribute it and/or
7+
# modify it under the terms of the GNU Lesser General Public
8+
# License as published by the Free Software Foundation; either
9+
# version 2.1 of the License, or (at your option) any later version.
10+
#
11+
# This library is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
# Lesser General Public License for more details.
15+
#
16+
# 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
20+
21+
"""Postcode (the Spanish postal code).
22+
23+
The Spanish postal code consists of five digits where the first two digits,
24+
ranging 01 to 52, correspond either to one of the 50 provinces of Spain or to
25+
one of the two autonomous cities on the African coast.
26+
27+
More information:
28+
29+
* https://en.wikipedia.org/wiki/Postal_codes_in_Spain
30+
31+
>>> validate('01000')
32+
'01000'
33+
>>> validate('52000')
34+
'52000'
35+
>>> validate('00000')
36+
Traceback (most recent call last):
37+
...
38+
InvalidComponent: ...
39+
>>> validate('53000')
40+
Traceback (most recent call last):
41+
...
42+
InvalidComponent: ...
43+
>>> validate('99999')
44+
Traceback (most recent call last):
45+
...
46+
InvalidComponent: ...
47+
>>> validate('5200')
48+
Traceback (most recent call last):
49+
...
50+
InvalidLength: ...
51+
>>> validate('520000')
52+
Traceback (most recent call last):
53+
...
54+
InvalidLength: ...
55+
"""
56+
57+
58+
from stdnum.exceptions import *
59+
from stdnum.util import clean, isdigits
60+
61+
62+
def compact(number):
63+
"""Convert the number to the minimal representation."""
64+
return clean(number, ' ').strip()
65+
66+
67+
def validate(number):
68+
"""Check if the number provided is a valid postal code."""
69+
number = compact(number)
70+
if len(number) != 5:
71+
raise InvalidLength()
72+
if not isdigits(number):
73+
raise InvalidFormat()
74+
if not '01' <= number[:2] <= '52':
75+
raise InvalidComponent()
76+
return number
77+
78+
79+
def is_valid(number):
80+
"""Check if the number provided is a valid postal code."""
81+
try:
82+
return bool(validate(number))
83+
except ValidationError:
84+
return False

0 commit comments

Comments
 (0)