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 RutValidator

assert RutValidator.is_valid("12.345.678-5")
assert not RutValidator.is_valid("12.345.678-0")
assert not RutValidator.is_valid(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.orm.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.orm.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.orm.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.orm.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.orm.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)

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.0.1.tar.gz (14.4 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.0.1-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rut_validator-1.0.1.tar.gz
  • Upload date:
  • Size: 14.4 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.0.1.tar.gz
Algorithm Hash digest
SHA256 e3a14a11f2e65d9a2a5afaeca211613038fb4785f639541f782c0a39e6d7e217
MD5 e02fc4343320b5395ed338f251fe7e56
BLAKE2b-256 0ec0a1906efda7c27b8ce6ce90231a1a7e9deb35a15e00756347a0f02e9f78f2

See more details on using hashes here.

Provenance

The following attestation bundles were made for rut_validator-1.0.1.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.0.1-py3-none-any.whl.

File metadata

  • Download URL: rut_validator-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 18.6 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.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2b177c040ed7a81d39e7ffb01f871d97039ba8693c01492b3df040f08d4d371d
MD5 5ab20d8ba26b89b12632535e346e1a0d
BLAKE2b-256 eb704e4ee57bfd2d59fbc21a4c7295a07870ce2455bca9b38b2d665e9d8d04d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for rut_validator-1.0.1-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

1.1.0

2 files

This release

1.0.1 This release

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