From da9a0fa2ae5b1f1d62af6ebd2aa16028216578d0 Mon Sep 17 00:00:00 2001 From: Apeksha_2022 <70309060+Apeksha9389@users.noreply.github.com> Date: Sun, 18 Oct 2026 02:48:33 +0530 Subject: [PATCH] Require ASCII digit bodies in Spanish identifiers --- src/validators/i18n/es.py | 4 ++-- tests/i18n/test_es.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/validators/i18n/es.py b/src/validators/i18n/es.py index 3d4b1ba3..c2761708 100644 --- a/src/validators/i18n/es.py +++ b/src/validators/i18n/es.py @@ -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 @@ -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 diff --git a/tests/i18n/test_es.py b/tests/i18n/test_es.py index 5b1ce013..dc227726 100644 --- a/tests/i18n/test_es.py +++ b/tests/i18n/test_es.py @@ -1,5 +1,7 @@ """Test i18n/es.""" +from typing import Callable + # external import pytest @@ -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)