Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/validators/i18n/es.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

def _nif_nie_validation(value: str, number_by_letter: Dict[str, str]):
"""Validate if the doi is a NIF or a NIE."""
if len(value) != 9:
if len(value) != 9 or not value.isascii():
return False
value = value.upper()
table = "TRWAGMYFPDXBNJZSQVHLCKE"
# If it is not a DNI, convert the first
# letter to the corresponding digit
numbers = number_by_letter.get(value[0], value[0]) + value[1:8]
# doi[8] is control
return numbers.isascii() and numbers.isdigit() and value[8] == table[int(numbers) % 23]
return numbers.isdigit() and value[8] == table[int(numbers) % 23]


@validator
Expand Down Expand Up @@ -51,14 +51,14 @@ def es_cif(value: str, /):
(Literal[True]): If `value` is a valid DOI string.
(ValidationError): If `value` is an invalid DOI string.
"""
if not value or len(value) != 9:
if not value or len(value) != 9 or not value.isascii():
return False
value = value.upper()
table = "JABCDEFGHI"
first_chr = value[0]
doi_body = value[1:8]
control = value[8]
if not doi_body.isascii() or not doi_body.isdigit():
if not doi_body.isdigit():
return False
res = (
10
Expand Down
22 changes: 22 additions & 0 deletions tests/i18n/test_es.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,25 @@ def test_spanish_identifiers_reject_non_ascii_digits(
translated = value[:-1].translate(str.maketrans("0123456789", digits)) + value[-1]
assert validate(value) is True
assert isinstance(validate(translated), ValidationError)


@pytest.mark.parametrize(
"validate,value,malformed",
[
(es_nif, "00000038S", "00000038\u017f"),
(es_nif, "00000038S", "00000038\u00df"),
(es_nie, "X0000038S", "X0000038\u017f"),
(es_nie, "X0000038S", "X0000038\u00df"),
(es_cif, "S0000000J", "\u017f0000000J"),
(es_doi, "00000038S", "00000038\u017f"),
(es_doi, "X0000038S", "X0000038\u00df"),
(es_doi, "S0000000J", "\u017f0000000J"),
],
)
def test_spanish_identifiers_reject_non_ascii_letters(
validate: Callable[[str], object], value: str, malformed: str
):
"""Case conversion must not turn non-ASCII letters into valid identifiers."""
assert validate(value) is True
assert validate(value[:-1] + value[-1].lower()) is True
assert isinstance(validate(malformed), ValidationError)