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.
  • Small functional validation API.
  • 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 examples and a description to OpenAPI. Runtime validation additionally verifies the modulo-11 check digit.

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 typing import Annotated

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: Annotated[
        RutSQLModel,
        rut_sqlmodel_field(unique=True, index=True),
    ]

Version 2 integrations live only under rut_validator.integrations; see the version 2 migration guide when upgrading from 1.x.

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 basedpyright
poetry run actionlint
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-2.0.0.tar.gz (13.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-2.0.0-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: rut_validator-2.0.0.tar.gz
  • Upload date:
  • Size: 13.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-2.0.0.tar.gz
Algorithm Hash digest
SHA256 5a5cb262015eee529348ff417c7ab232e90e7559e95894a18c37051271e05ea6
MD5 632c2f60c78dc44481f26a0fa59ad252
BLAKE2b-256 3505c5a215df818ce9f3e2f41cd00588610d89bc3d56043e4fa4dfaaadd7fcef

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: rut_validator-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 15.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-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d6dfe15b2c7d9acbbd82f132ec05812e3018aab67121cd5b30280135537dd3d9
MD5 99dfe7808d5eec0a94344b207390f782
BLAKE2b-256 8b95c3fc625e8e46b803eeca183f5735a10a30c8961b859fe958d689f0b3c7e6

See more details on using hashes here.

Provenance

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

This release

2.0.0 This release

2 files

1.1.0

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