Skip to main content

rut-validator

PyPI version Python versions License: MIT CI codecov Documentation Status

Framework-agnostic Chilean RUT validation with optional integrations for Pydantic, FastAPI, Django, SQLAlchemy, and SQLModel.

Features

  • Strict modulo-11 check digit validation.
  • Formatted, hyphenated, and normalized input detection.
  • Immutable and hashable Rut value object.
  • Functional and class-based validation APIs.
  • Structured errors with stable machine-readable codes.
  • CLI validation, formatting, inspection, and batch processing.
  • Normalized persistence through optional ORM adapters.
  • Strict ASCII input handling.
  • Python 3.10 through 3.14 support.

Validation confirms syntax and the check digit. It does not confirm that a RUT exists, is active, or belongs to a particular person or organization. Values shown below are synthetic validation fixtures and must not be interpreted as identifying real people.

Installation

Install the standalone API and CLI:

pip install rut-validator

Install only the integrations your application needs:

pip install "rut-validator[pydantic]"
pip install "rut-validator[fastapi]"
pip install "rut-validator[django]"
pip install "rut-validator[sqlalchemy]"
pip install "rut-validator[sqlmodel]"
pip install "rut-validator[all]"

Basic usage

from rut_validator import calculate_check_digit, validate_rut

rut = validate_rut("20.884.437-7")

assert rut.normalized == "208844377"
assert rut.formatted == "20.884.437-7"
assert rut.hyphenated == "20884437-7"
assert rut.body == 20884437
assert rut.check_digit == "7"
assert calculate_check_digit("20884437") == "7"

For boolean-only checks:

from rut_validator import is_valid_rut

assert is_valid_rut("12.345.678-5")
assert not is_valid_rut("12.345.678-0")
assert not is_valid_rut(None)

Error handling

from rut_validator import RutValidationError, validate_rut

try:
    validate_rut("12.345.678-0")
except RutValidationError as error:
    print(error.code)
    print(error.as_dict())

Public error codes are invalid_value, invalid_format, and invalid_check_digit.

Pydantic

from pydantic import BaseModel

from rut_validator.integrations.pydantic import RutPydantic


class User(BaseModel):
    name: str
    rut: RutPydantic


user = User(name="Ana", rut="12.345.678-5")
assert user.rut == "123456785"

FastAPI

from fastapi import FastAPI
from pydantic import BaseModel

from rut_validator.integrations.pydantic import RutPydantic

app = FastAPI()


class Person(BaseModel):
    rut: RutPydantic


@app.post("/people")
def create_person(person: Person) -> Person:
    return person

Invalid values produce FastAPI's standard HTTP 422 response, and RutPydantic contributes its pattern and examples to OpenAPI.

Django

from django.db import models

from rut_validator.integrations.django import RutDjango


class Person(models.Model):
    name = models.CharField(max_length=100)
    rut = RutDjango(unique=True)

The field accepts formatted input and stores the normalized nine-character representation.

SQLAlchemy

from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

from rut_validator.integrations.sqlalchemy import RutSQLAlchemy


class Base(DeclarativeBase):
    pass


class Person(Base):
    __tablename__ = "people"

    id: Mapped[int] = mapped_column(primary_key=True)
    rut: Mapped[str] = mapped_column(RutSQLAlchemy(), unique=True)

RutSQLAlchemy validates values both before persistence and after database reads.

SQLModel

from sqlmodel import Field, SQLModel

from rut_validator.integrations.sqlmodel import RutSQLModel, rut_sqlmodel_field


class Person(SQLModel, table=True):
    id: int | None = Field(default=None, primary_key=True)
    rut: RutSQLModel = rut_sqlmodel_field(unique=True, index=True)

Imports under rut_validator.orm remain available with deprecation warnings during the 1.x migration window. New code should use rut_validator.integrations; see the version 2 migration guide.

CLI

rut-validator validate 12.345.678-5
rut-validator validate 12.345.678-5 --json
rut-validator format 123456785 --format formatted
rut-validator info 12.345.678-5 --detailed
rut-validator batch ruts.txt --output result.jsonl

Documentation

Read the published documentation or serve it locally:

poetry install --all-extras
poetry run mkdocs serve

Examples

Development

git clone https://github.com/ezer-mackenzie/rut-validator.git
cd rut-validator
poetry install --all-extras

poetry run pytest --cov=rut_validator
poetry run ruff check src tests examples
poetry run black --check src tests examples
poetry run mypy src/rut_validator
poetry run mkdocs build --strict
poetry build

Install the Git hooks with:

poetry run pre-commit install

See CONTRIBUTING.md for the contribution workflow.

Security

Report security issues according to SECURITY.md. Do not include real personal RUT values in public bug reports or test fixtures.

License

Licensed under the MIT License.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rut_validator-1.1.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

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

rut_validator-1.1.0-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

Details for the file rut_validator-1.1.0.tar.gz.

File metadata

  • Download URL: rut_validator-1.1.0.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for rut_validator-1.1.0.tar.gz
Algorithm Hash digest
SHA256 e0ca2efbfe9606dff54c9af04b9eb9e94f0a0a40c2c1e7299b1408d2059bd871
MD5 0412fef3d45fb5ee9053477286346688
BLAKE2b-256 ee0e590c9e3537da05111d42ee5f70b7635b7d5b6f7703a102050f156784d24e

See more details on using hashes here.

Provenance

The following attestation bundles were made for rut_validator-1.1.0.tar.gz:

Publisher: publish.yml on ezer-mackenzie/rut-validator

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

File details

Details for the file rut_validator-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: rut_validator-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for rut_validator-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9c416313cf3ddc4acc09e4bb9434363ff17043fc03b32add84c07e9eff20ed18
MD5 6ce634e4e6abc09f999a2d5e39d954d5
BLAKE2b-256 3038e11d2b63776929f9442c905be408ffc7aea26e010db3ca420056294eea77

See more details on using hashes here.

Provenance

The following attestation bundles were made for rut_validator-1.1.0-py3-none-any.whl:

Publisher: publish.yml on ezer-mackenzie/rut-validator

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

Release history Release notifications | RSS feed

2.0.0

2 files

This release

1.1.0 This release

2 files

1.0.1

2 files

1.0.0

2 files

0.9.0

2 files

0.8.0

2 files

0.7.0

2 files

0.6.0

2 files

0.5.0

2 files

0.4.0

2 files

0.3.0

2 files

0.2.0

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page