Skip to content

Commit 440b1cd

Browse files
authored
feat: adds Country Code validation (#280)
- ignore flake8 line length in country_code.py
1 parent 60c5314 commit 440b1cd

File tree

4 files changed

+199
-0
lines changed

4 files changed

+199
-0
lines changed

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
docstring-convention = google
55
exclude = __pycache__,.github,.pytest_cache,.tox,.venv,.vscode,site
66
max-line-length = 100
7+
per-file-ignores = validators/country_code.py:E501

tests/test_country_code.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# external
2+
import pytest
3+
4+
# local
5+
from validators import country_code, ValidationFailure
6+
7+
8+
@pytest.mark.parametrize(
9+
("value", "iso_format"),
10+
[
11+
("ISR", "auto"),
12+
("US", "alpha2"),
13+
("USA", "alpha3"),
14+
("840", "numeric"),
15+
],
16+
)
17+
def test_returns_true_on_valid_country_code(value: str, iso_format: str):
18+
"""Test returns true on valid country code."""
19+
assert country_code(value, iso_format=iso_format)
20+
21+
22+
@pytest.mark.parametrize(
23+
("value", "iso_format"),
24+
[
25+
(None, "auto"),
26+
("", "auto"),
27+
("123456", "auto"),
28+
("XY", "alpha2"),
29+
("PPP", "alpha3"),
30+
("123", "numeric"),
31+
("us", "auto"),
32+
("uSa", "auto"),
33+
("US ", "auto"),
34+
("U.S", "auto"),
35+
("1ND", "unknown"),
36+
("ISR", None),
37+
],
38+
)
39+
def test_returns_failed_validation_on_invalid_country_code(value: str, iso_format: str):
40+
"""Test returns failed validation on invalid country code."""
41+
assert isinstance(country_code(value, iso_format=iso_format), ValidationFailure)

validators/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from .between import between
1212
from .btc_address import btc_address
1313
from .card import amex, card_number, diners, discover, jcb, mastercard, unionpay, visa
14+
from .country_code import country_code
1415
from .domain import domain
1516
from .email import email
1617
from .hashes import md5, sha1, sha224, sha256, sha512
@@ -33,6 +34,7 @@
3334
"btc_address",
3435
"eth_address",
3536
"card_number",
37+
"country_code",
3638
"diners",
3739
"discover",
3840
"domain",

validators/country_code.py

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
"""Country Codes."""
2+
# -*- coding: utf-8 -*-
3+
4+
# local
5+
from validators.utils import validator
6+
7+
# fmt: off
8+
alpha_2 = [
9+
"AD", "AE", "AF", "AG", "AI", "AL", "AM", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AX", "AZ",
10+
"BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BL", "BM", "BN", "BO", "BQ", "BR", "BS", "BT", "BV", "BW", "BY", "BZ",
11+
"CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU", "CV", "CW", "CX", "CY", "CZ",
12+
"DE", "DJ", "DK", "DM", "DO", "DZ",
13+
"EC", "EE", "EG", "EH", "ER", "ES", "ET",
14+
"FI", "FJ", "FK", "FM", "FO", "FR",
15+
"GA", "GB", "GD", "GE", "GF", "GG", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY",
16+
"HK", "HM", "HN", "HR", "HT", "HU",
17+
"ID", "IE", "IL", "IM", "IN", "IO", "IQ", "IR", "IS", "IT",
18+
"JE", "JM", "JO", "JP", "KE", "KG", "KH", "KI",
19+
"KM", "KN", "KP", "KR", "KW", "KY", "KZ",
20+
"LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY",
21+
"MA", "MC", "MD", "ME", "MF", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ",
22+
"NA", "NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ",
23+
"OM",
24+
"PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY",
25+
"QA",
26+
"RE", "RO", "RS", "RU", "RW",
27+
"SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "SS", "ST", "SV", "SX", "SY", "SZ",
28+
"TC", "TD", "TF", "TG", "TH", "TJ", "TK", "TL", "TM", "TN", "TO", "TR", "TT", "TV", "TW", "TZ",
29+
"UA", "UG", "UM", "US", "UY", "UZ",
30+
"VC", "VE", "VG", "VI", "VN", "VU",
31+
"WF", "WS",
32+
"YE", "YT",
33+
"ZA", "ZM", "ZW",
34+
]
35+
alpha_3 = [
36+
"ABW", "AFG", "AGO", "AIA", "ALA", "ALB", "AND", "ARE", "ARG", "ARM", "ASM", "ATA", "ATF", "ATG", "AUS", "AUT", "AZE", "BDI", "BEL", "BEN", "BES", "BFA", "BGD", "BGR", "BHR", "BHS", "BIH", "BLM", "BLR", "BLZ", "BMU", "BOL", "BRA", "BRB", "BRN", "BTN", "BVT", "BWA",
37+
"CAF", "CAN", "CCK", "CHE", "CHL", "CHN", "CIV", "CMR", "COD", "COG", "COK", "COL", "COM", "CPV", "CRI", "CUB", "CUW", "CXR", "CYM", "CYP", "CZE",
38+
"DEU", "DJI", "DMA", "DNK", "DOM", "DZA",
39+
"ECU", "EGY", "ERI", "ESH", "ESP", "EST", "ETH",
40+
"FIN", "FJI", "FLK", "FRA", "FRO", "FSM",
41+
"GAB", "GBR", "GEO", "GGY", "GHA", "GIB", "GIN", "GLP", "GMB", "GNB", "GNQ", "GRC", "GRD", "GRL", "GTM", "GUF", "GUM", "GUY",
42+
"HKG", "HMD", "HND", "HRV", "HTI", "HUN",
43+
"IDN", "IMN", "IND", "IOT", "IRL", "IRN", "IRQ", "ISL", "ISR", "ITA",
44+
"JAM", "JEY", "JOR", "JPN",
45+
"KAZ", "KEN", "KGZ", "KHM", "KIR", "KNA", "KOR", "KWT",
46+
"LAO", "LBN", "LBR", "LBY", "LCA", "LIE", "LKA", "LSO", "LTU", "LUX", "LVA",
47+
"MAC", "MAF", "MAR", "MCO", "MDA", "MDG", "MDV", "MEX", "MHL", "MKD", "MLI", "MLT", "MMR", "MNE", "MNG", "MNP", "MOZ", "MRT", "MSR", "MTQ", "MUS", "MWI", "MYS", "MYT",
48+
"NAM", "NCL", "NER", "NFK", "NGA", "NIC", "NIU", "NLD", "NOR", "NPL", "NRU", "NZL",
49+
"OMN",
50+
"PAK", "PAN", "PCN", "PER", "PHL", "PLW", "PNG", "POL", "PRI", "PRK", "PRT", "PRY", "PSE", "PYF",
51+
"QAT",
52+
"REU", "ROU", "RUS", "RWA",
53+
"SAU", "SDN", "SEN", "SGP", "SGS", "SHN", "SJM", "SLB", "SLE", "SLV", "SMR", "SOM", "SPM", "SRB", "SSD", "STP", "SUR", "SVK", "SVN", "SWE", "SWZ", "SXM", "SYC", "SYR",
54+
"TCA", "TCD", "TGO", "THA", "TJK", "TKL", "TKM", "TLS", "TON", "TTO", "TUN", "TUR", "TUV", "TWN", "TZA",
55+
"UGA", "UKR", "UMI", "URY", "USA", "UZB",
56+
"VCT", "VEN", "VGB", "VIR", "VNM", "VUT",
57+
"WLF", "WSM",
58+
"YEM",
59+
"ZAF", "ZMB", "ZWE",
60+
]
61+
numeric = [
62+
"004", "008", "010", "012", "016", "020", "024", "028", "031", "032",
63+
"036", "040", "044", "048", "050", "051", "052", "056", "060", "064",
64+
"068", "070", "072", "074", "076", "084", "086", "090", "092", "096",
65+
"100", "104", "108", "112", "116", "120", "124", "132", "136", "140",
66+
"144", "148", "152", "156", "158", "162", "166", "170", "174", "175",
67+
"178", "180", "184", "188", "191", "192", "196", "203", "204", "208",
68+
"212", "214", "218", "222", "226", "231", "232", "233", "234", "238",
69+
"239", "242", "246", "248", "250", "254", "258", "260", "262", "266",
70+
"268", "270", "275", "276", "288", "292", "296", "300", "304", "308",
71+
"312", "316", "320", "324", "328", "332", "334", "340", "344", "348",
72+
"352", "356", "360", "364", "368", "372", "376", "380", "384", "388",
73+
"392", "398", "400", "404", "408", "410", "414", "417", "418", "422",
74+
"426", "428", "430", "434", "438", "440", "442", "446", "450", "454",
75+
"458", "462", "466", "470", "474", "478", "480", "484", "492", "496",
76+
"498", "499", "500", "504", "508", "512", "516", "520", "524", "528",
77+
"531", "533", "534", "535", "540", "548", "554", "558", "562", "566",
78+
"570", "574", "578", "580", "581", "583", "584", "585", "586", "591",
79+
"598", "600", "604", "608", "612", "616", "620", "624", "626", "630",
80+
"634", "638", "642", "643", "646", "652", "654", "659", "660", "662",
81+
"663", "666", "670", "674", "678", "682", "686", "688", "690", "694",
82+
"702", "703", "704", "705", "706", "710", "716", "724", "728", "729",
83+
"732", "740", "744", "748", "752", "756", "760", "762", "764", "768",
84+
"772", "776", "780", "784", "788", "792", "795", "796", "798", "800",
85+
"804", "807", "818", "826", "831", "832", "833", "834", "840", "850",
86+
"854", "858", "860", "862", "876", "882", "887", "894",
87+
]
88+
# fmt: on
89+
90+
91+
def get_code_type(format_type: str):
92+
"""Returns the type of country code."""
93+
if format_type.isdigit():
94+
return "numeric"
95+
if format_type.isalpha():
96+
if len(format_type) == 2:
97+
return "alpha2"
98+
if len(format_type) == 3:
99+
return "alpha3"
100+
return "invalid"
101+
102+
103+
@validator
104+
def country_code(value: str, /, *, iso_format: str = "auto"):
105+
"""Validates given country code.
106+
107+
Refer [ISO 3166 Standard][1].
108+
109+
[1]: https://www.iso.org/iso-3166-country-codes.html
110+
111+
Examples:
112+
>>> country_code('GB', iso_format='alpha3')
113+
# Output: False
114+
>>> country_code('USA')
115+
# Output: True
116+
>>> country_code('840', iso_format='numeric')
117+
# Output: True
118+
>>> country_code('iN', iso_format='alpha2')
119+
# Output: False
120+
>>> country_code('ZWE', iso_format='alpha3')
121+
# Output: True
122+
123+
Args:
124+
value:
125+
Country code string to validate.
126+
iso_format:
127+
ISO format to be used. Available options are:
128+
`alpha2`, `alpha3`, `numeric` & `auto` which
129+
automatically identifies the format.
130+
131+
Returns:
132+
(Literal[True]):
133+
If `value` is a valid country code.
134+
(ValidationFailure):
135+
If `value` is an invalid country code.
136+
137+
Note:
138+
- This function performs a case-sensitive validation.
139+
"""
140+
if not value:
141+
return False
142+
143+
if not (1 < len(value) < 4):
144+
return False
145+
146+
if iso_format == "auto" and (iso_format := get_code_type(value)) == "invalid":
147+
return False
148+
149+
if iso_format == "alpha2":
150+
return value in alpha_2
151+
if iso_format == "alpha3":
152+
return value in alpha_3
153+
if iso_format == "numeric":
154+
return value in numeric
155+
return False

0 commit comments

Comments
 (0)