Pydantic validators for spanish identification codes: NIF, DNI and NIE.
Project description
spanish-nif
⚠️ AI-generated library: This library may contain severe vulnerabilities and should not be trusted for critical workflows. Use at your own risk; it was produced with the Codex AI assistant.
This library turns Spanish identification numbers into first-class Python types.
Each class (NIF, DNI, NIE) subclasses str, validates its control letter on
construction, and plugs straight into Pydantic so you can drop it into your
data models without writing bespoke validators.
Installation
-
pip
python -m pip install spanish-nif
-
uv
uv pip install spanish-nif
Quick examples
from spanish_nif import DNI, NIE, NIF
DNI("12345678Z") # returns a validated DNI string
NIE.is_valid("X1234567L") # -> True
NIF("K0867756N").variant # -> "legacy"
from spanish_nif import DNI, NIF
fresh_dni = DNI.random()
some_nif = NIF.random() # variant chosen automatically
legacy_nif = NIF.random(variant="legacy")
from pydantic import BaseModel
from spanish_nif import NIF
class TaxPayer(BaseModel):
nif: NIF
tax_payer = TaxPayer(nif="12345678Z")
assert tax_payer.nif == "12345678Z"
- Invalid inputs raise an
InvalidIdentificationsubclass with a helpful message. - Normalisation uppercases the value and validates the control-letter sequence; inputs must already be correctly formatted.
- Need dummy data for tests or demos? Each class exposes a
.random()helper; pass arandom.Randominstance only if you require deterministic output.
Related Projects
- validarnif – Python module exposing procedural validators for NIF/NIE/CIF and optional preprocessing utilities; spanish-nif instead wraps those rules in reusable string subclasses with Pydantic/JSON Schema integration.
- spanish-dni – Python package with validator functions and generators focused on list processing; our library prioritises strong typing and schema metadata for application models.
- spain-id – TypeScript/Node toolkit validating NIF/NIE/CIF in browser and server environments, whereas spanish-nif targets Python workflows.
- ulabox/nif-validator – PHP utility offering static
isValid*helpers for CIF, DNI and NIE; our package emphasises typed models and Pydantic interoperability rather than imperative checks. - criptalia/spanish_dni_validator – Go port of the ulabox/NIF validator covering DNI, NIE and CIF with
IsValid*helpers; spanish-nif instead provides Python string subclasses with declarative Pydantic integration.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spanish_nif-0.4.0.tar.gz.
File metadata
- Download URL: spanish_nif-0.4.0.tar.gz
- Upload date:
- Size: 99.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d2185137c07660b638d71da01a4349cdae6870684ea2c389c1e782e55619b369
|
|
| MD5 |
0c7dc9f70fe1d3eb3f86bcd2296334cc
|
|
| BLAKE2b-256 |
c2459562d04ab78e313987855763ff7f2299d3e80491b129d05f0731b4ba564f
|
Provenance
The following attestation bundles were made for spanish_nif-0.4.0.tar.gz:
Publisher:
publish.yml on haztecaso/spanish-nif
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spanish_nif-0.4.0.tar.gz -
Subject digest:
d2185137c07660b638d71da01a4349cdae6870684ea2c389c1e782e55619b369 - Sigstore transparency entry: 660846054
- Sigstore integration time:
-
Permalink:
haztecaso/spanish-nif@497545b14d2b0f2ea90375ea5c1fc8e14d005353 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/haztecaso
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@497545b14d2b0f2ea90375ea5c1fc8e14d005353 -
Trigger Event:
push
-
Statement type:
File details
Details for the file spanish_nif-0.4.0-py3-none-any.whl.
File metadata
- Download URL: spanish_nif-0.4.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b96732afc30dec9bdb53feca97a678524e348915617fd04bd2b990c71885660d
|
|
| MD5 |
400c71dc37809905651904629051f86e
|
|
| BLAKE2b-256 |
86e6d1105948730f74ccf45537e795b18cbffac334bd871d9a9dfa29eb8c01e8
|
Provenance
The following attestation bundles were made for spanish_nif-0.4.0-py3-none-any.whl:
Publisher:
publish.yml on haztecaso/spanish-nif
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spanish_nif-0.4.0-py3-none-any.whl -
Subject digest:
b96732afc30dec9bdb53feca97a678524e348915617fd04bd2b990c71885660d - Sigstore transparency entry: 660846058
- Sigstore integration time:
-
Permalink:
haztecaso/spanish-nif@497545b14d2b0f2ea90375ea5c1fc8e14d005353 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/haztecaso
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@497545b14d2b0f2ea90375ea5c1fc8e14d005353 -
Trigger Event:
push
-
Statement type: