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.

Documentation


⚠️ 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 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.4.0.tar.gz (99.4 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.4.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

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

Hashes for spanish_nif-0.4.0.tar.gz
Algorithm Hash digest
SHA256 d2185137c07660b638d71da01a4349cdae6870684ea2c389c1e782e55619b369
MD5 0c7dc9f70fe1d3eb3f86bcd2296334cc
BLAKE2b-256 c2459562d04ab78e313987855763ff7f2299d3e80491b129d05f0731b4ba564f

See more details on using hashes here.

Provenance

The following attestation bundles were made for spanish_nif-0.4.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.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

Hashes for spanish_nif-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b96732afc30dec9bdb53feca97a678524e348915617fd04bd2b990c71885660d
MD5 400c71dc37809905651904629051f86e
BLAKE2b-256 86e6d1105948730f74ccf45537e795b18cbffac334bd871d9a9dfa29eb8c01e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for spanish_nif-0.4.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