Skip to main content

Pydantic validators for spanish identification codes: NIF, DNI and NIE.

Project description

spanish-nif

Pydantic validators for spanish identification codes: NIF, DNI and NIE.

test badge Supported Python versions PyPI version Static Badge

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.

⚠️ 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.

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  # -> "klm"
from spanish_nif import DNI, NIF

fresh_dni = DNI.random()
some_nif = NIF.random()          # variant chosen automatically
klm_nif = NIF.random(variant="klm")
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 InvalidIdentification subclass 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 a random.Random instance 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

spanish_nif-0.5.0.tar.gz (99.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spanish_nif-0.5.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file spanish_nif-0.5.0.tar.gz.

File metadata

  • Download URL: spanish_nif-0.5.0.tar.gz
  • Upload date:
  • Size: 99.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spanish_nif-0.5.0.tar.gz
Algorithm Hash digest
SHA256 220b5b706602cd13b1b55ef691253c6b675a8b57c5e52f3511617f442b61a11b
MD5 9535676624def9b233148edc3f80743b
BLAKE2b-256 c71c73ade3a2cfadcb99eab8f98783d814473c7608e9a2e1a754c9b49981e068

See more details on using hashes here.

Provenance

The following attestation bundles were made for spanish_nif-0.5.0.tar.gz:

Publisher: publish.yml on haztecaso/spanish-nif

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file spanish_nif-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: spanish_nif-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for spanish_nif-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 235fcd830d3418e6c833bcef0a6daeb3485954b9206eab5ea6d6ed7fc66ea6c3
MD5 a23022721057d55a0628b024e7e3efd0
BLAKE2b-256 c700bc9ac3162ac129fa22cc49a3203ac5c2af7a97d2a2dffeb0ba463a4e5215

See more details on using hashes here.

Provenance

The following attestation bundles were made for spanish_nif-0.5.0-py3-none-any.whl:

Publisher: publish.yml on haztecaso/spanish-nif

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page