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
4 changes: 2 additions & 2 deletions src/validators/i18n/es.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def _nif_nie_validation(value: str, number_by_letter: Dict[str, str]):
# letter to the corresponding digit
numbers = number_by_letter.get(value[0], value[0]) + value[1:8]
# doi[8] is control
return numbers.isdigit() and value[8] == table[int(numbers) % 23]
return numbers.isascii() and numbers.isdigit() and value[8] == table[int(numbers) % 23]


@validator
Expand Down Expand Up @@ -58,7 +58,7 @@ def es_cif(value: str, /):
first_chr = value[0]
doi_body = value[1:8]
control = value[8]
if not doi_body.isdigit():
if not doi_body.isascii() or not doi_body.isdigit():
return False
res = (
10
Expand Down
23 changes: 23 additions & 0 deletions tests/i18n/test_es.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Test i18n/es."""

from typing import Callable

# external
import pytest

Expand Down Expand Up @@ -123,3 +125,24 @@ def test_returns_false_on_invalid_nif():
def test_returns_true_on_valid_doi(value: str):
"""Test returns true on valid doi."""
assert es_doi(value)


@pytest.mark.parametrize("digits", ["0123456789", "٠١٢٣٤٥٦٧٨٩", "०१२३४५६७८९"])
@pytest.mark.parametrize(
"validate,value",
[
(es_nif, "26643189N"),
(es_nie, "X0095892M"),
(es_cif, "B25162520"),
(es_doi, "26643189N"),
(es_doi, "X0095892M"),
(es_doi, "B25162520"),
],
)
def test_spanish_identifiers_reject_non_ascii_digits(
validate: Callable[[str], object], value: str, digits: str
):
"""Reject lookalike digit bodies even when their numeric checksums agree."""
translated = value[:-1].translate(str.maketrans("0123456789", digits)) + value[-1]
assert validate(value) is True
assert isinstance(validate(translated), ValidationError)