Skip to content

Commit 1b6e72f

Browse files
committed
Require ASCII digits in French department numbers
fr_department() converted string input with int(), which also accepts surrounding whitespace, a sign, underscores and non-ASCII decimal digits, so values such as " 75", "+75", "7_5" or fullwidth "75" validated. Match the string against [0-9]+ before converting it.
1 parent 22786cc commit 1b6e72f

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

‎src/validators/i18n/fr.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ def fr_department(value: typing.Union[str, int]):
5757
if isinstance(value, str):
5858
if value in ("2A", "2B"): # Corsica
5959
return True
60-
try:
61-
value = int(value)
62-
except ValueError:
60+
# int() also takes signs, underscores, surrounding whitespace and non-ASCII digits
61+
if not re.fullmatch(r"[0-9]+", value):
6362
return False
63+
value = int(value)
6464
return 1 <= value <= 19 or 21 <= value <= 95 or 971 <= value <= 976 # Overseas departments
6565

6666

‎tests/i18n/test_fr.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ def test_returns_true_on_valid_department(value: Union[str, int]):
8080
("96",),
8181
("20",),
8282
(20,),
83+
(" 75",),
84+
("75\n",),
85+
("+75",),
86+
("7_5",),
87+
("75",), # fullwidth digits
88+
("٧٥",), # Arabic-Indic digits
8389
],
8490
)
8591
def test_returns_failed_validation_on_invalid_department(value: Union[str, int]):

0 commit comments

Comments
 (0)