Skip to content

Fix: Allow Special DOI Cases Used in Public Administration Tests #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 26, 2025
Merged
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
2 changes: 1 addition & 1 deletion src/validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
fr_ssn,
ind_aadhar,
ind_pan,
ru_inn
ru_inn,
)
from .iban import iban
from .ip_address import ipv4, ipv6
Expand Down
11 changes: 5 additions & 6 deletions src/validators/i18n/es.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""Spain."""

# standard
from typing import Dict, Set
from typing import Dict

# local
from validators.utils import validator


def _nif_nie_validation(value: str, number_by_letter: Dict[str, str], special_cases: Set[str]):
def _nif_nie_validation(value: str, number_by_letter: Dict[str, str]):
"""Validate if the doi is a NIF or a NIE."""
if value in special_cases or len(value) != 9:
if len(value) != 9:
return False
value = value.upper()
table = "TRWAGMYFPDXBNJZSQVHLCKE"
Expand Down Expand Up @@ -104,8 +104,7 @@ def es_nif(value: str, /):
(ValidationError): If `value` is an invalid DOI string.
"""
number_by_letter = {"L": "0", "M": "0", "K": "0"}
special_cases = {"X0000000T", "00000000T", "00000001R"}
return _nif_nie_validation(value, number_by_letter, special_cases)
return _nif_nie_validation(value, number_by_letter)


@validator
Expand Down Expand Up @@ -137,7 +136,7 @@ def es_nie(value: str, /):
number_by_letter = {"X": "0", "Y": "1", "Z": "2"}
# NIE must must start with X Y or Z
if value and value[0] in number_by_letter:
return _nif_nie_validation(value, number_by_letter, {"X0000000T"})
return _nif_nie_validation(value, number_by_letter)
return False


Expand Down
2 changes: 1 addition & 1 deletion src/validators/uri.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def uri(value: str, /):

# email
if value.startswith("mailto:"):
return email(value[len("mailto:"):])
return email(value[len("mailto:") :])

# file
if value.startswith("file:"):
Expand Down
16 changes: 5 additions & 11 deletions tests/i18n/test_es.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,9 @@ def test_returns_true_on_valid_nif(value: str):
assert es_nif(value)


@pytest.mark.parametrize(
("value",),
[
("12345",),
("X0000000T",),
("00000000T",),
("00000001R",),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these are valid, can you please put them in the valid nif test cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

],
)
def test_returns_false_on_invalid_nif(value: str):
def test_returns_false_on_invalid_nif():
"""Test returns false on invalid nif."""
result = es_nif(value)
result = es_nif("12345")
assert isinstance(result, ValidationError)


Expand All @@ -117,10 +108,13 @@ def test_returns_false_on_invalid_nif(value: str):
("U4839822F",),
("B96817697",),
# NIEs
("X0000000T",),
("X0095892M",),
("X8868108K",),
("X2911154K",),
# NIFs
("00000001R",),
("00000000T",),
("26643189N",),
("07060225F",),
("49166693F",),
Expand Down
Loading