Skip to content

Commit 143372e

Browse files
committed
Reject non-ASCII letters in Spanish identifiers
1 parent 22786cc commit 143372e

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎src/validators/i18n/es.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99

1010
def _nif_nie_validation(value: str, number_by_letter: Dict[str, str]):
1111
"""Validate if the doi is a NIF or a NIE."""
12-
if len(value) != 9:
12+
if len(value) != 9 or not value.isascii():
1313
return False
1414
value = value.upper()
1515
table = "TRWAGMYFPDXBNJZSQVHLCKE"
1616
# If it is not a DNI, convert the first
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.isascii() and numbers.isdigit() and value[8] == table[int(numbers) % 23]
20+
return numbers.isdigit() and value[8] == table[int(numbers) % 23]
2121

2222

2323
@validator
@@ -51,14 +51,14 @@ def es_cif(value: str, /):
5151
(Literal[True]): If `value` is a valid DOI string.
5252
(ValidationError): If `value` is an invalid DOI string.
5353
"""
54-
if not value or len(value) != 9:
54+
if not value or len(value) != 9 or not value.isascii():
5555
return False
5656
value = value.upper()
5757
table = "JABCDEFGHI"
5858
first_chr = value[0]
5959
doi_body = value[1:8]
6060
control = value[8]
61-
if not doi_body.isascii() or not doi_body.isdigit():
61+
if not doi_body.isdigit():
6262
return False
6363
res = (
6464
10

‎tests/i18n/test_es.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,25 @@ def test_spanish_identifiers_reject_non_ascii_digits(
153153
translated = value[:-1].translate(str.maketrans("0123456789", digits)) + value[-1]
154154
assert validate(value) is True
155155
assert isinstance(validate(translated), ValidationError)
156+
157+
158+
@pytest.mark.parametrize(
159+
"validate,value,malformed",
160+
[
161+
(es_nif, "00000038S", "00000038\u017f"),
162+
(es_nif, "00000038S", "00000038\u00df"),
163+
(es_nie, "X0000038S", "X0000038\u017f"),
164+
(es_nie, "X0000038S", "X0000038\u00df"),
165+
(es_cif, "S0000000J", "\u017f0000000J"),
166+
(es_doi, "00000038S", "00000038\u017f"),
167+
(es_doi, "X0000038S", "X0000038\u00df"),
168+
(es_doi, "S0000000J", "\u017f0000000J"),
169+
],
170+
)
171+
def test_spanish_identifiers_reject_non_ascii_letters(
172+
validate: Callable[[str], object], value: str, malformed: str
173+
):
174+
"""Case conversion must not turn non-ASCII letters into valid identifiers."""
175+
assert validate(value) is True
176+
assert validate(value[:-1] + value[-1].lower()) is True
177+
assert isinstance(validate(malformed), ValidationError)

0 commit comments

Comments
 (0)