Skip to content

Commit dce580d

Browse files
Apeksha9389nandgator
authored andcommitted
Require ASCII digit bodies in Spanish identifiers
1 parent a0a4eaf commit dce580d

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

‎src/validators/i18n/es.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def _nif_nie_validation(value: str, number_by_letter: Dict[str, str]):
1717
# letter to the corresponding digit
1818
numbers = number_by_letter.get(value[0], value[0]) + value[1:8]
1919
# doi[8] is control
20-
return numbers.isdigit() and value[8] == table[int(numbers) % 23]
20+
return numbers.isascii() and numbers.isdigit() and value[8] == table[int(numbers) % 23]
2121

2222

2323
@validator
@@ -58,7 +58,7 @@ def es_cif(value: str, /):
5858
first_chr = value[0]
5959
doi_body = value[1:8]
6060
control = value[8]
61-
if not doi_body.isdigit():
61+
if not doi_body.isascii() or not doi_body.isdigit():
6262
return False
6363
res = (
6464
10

‎tests/i18n/test_es.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Test i18n/es."""
22

3+
from typing import Callable
4+
35
# external
46
import pytest
57

@@ -123,3 +125,24 @@ def test_returns_false_on_invalid_nif():
123125
def test_returns_true_on_valid_doi(value: str):
124126
"""Test returns true on valid doi."""
125127
assert es_doi(value)
128+
129+
130+
@pytest.mark.parametrize("digits", ["0123456789", "٠١٢٣٤٥٦٧٨٩", "०१२३४५६७८९"])
131+
@pytest.mark.parametrize(
132+
"validate,value",
133+
[
134+
(es_nif, "26643189N"),
135+
(es_nie, "X0095892M"),
136+
(es_cif, "B25162520"),
137+
(es_doi, "26643189N"),
138+
(es_doi, "X0095892M"),
139+
(es_doi, "B25162520"),
140+
],
141+
)
142+
def test_spanish_identifiers_reject_non_ascii_digits(
143+
validate: Callable[[str], object], value: str, digits: str
144+
):
145+
"""Reject lookalike digit bodies even when their numeric checksums agree."""
146+
translated = value[:-1].translate(str.maketrans("0123456789", digits)) + value[-1]
147+
assert validate(value) is True
148+
assert isinstance(validate(translated), ValidationError)

0 commit comments

Comments
 (0)