Skip to main content

validonce: write rules once, enforce them everywhere

validonce (Python)

Write cross-field validation rules once on a Pydantic model. validonce enforces them on the server and exports them as data (x-validonce) for other evaluators (browser, .NET).

pip install validonce      # needs Python 3.10+ and pydantic>=2.9

Define a model

from datetime import date
from typing import Literal

from validonce import ValidonceModel, F, when


class Signup(ValidonceModel):
    plan: Literal["free", "pro"]
    company: str | None = None
    start: date
    end: date | None = None
    quantity: int = 1

    __rules__ = [
        when(F.plan == "pro").require(F.company, key="rules.company_required"),
        F.end.ge(F.start, key="rules.end_after_start"),
        F.quantity.ge(1, key="rules.qty_min", params={"min": 1}),
    ]
  • Rules run after field validation succeeds, on the wire form (model_dump(mode="json", by_alias=True)). One ValidationError lists every failing rule.
  • Each error sits on its target field, with type="validonce.<kind>", msg = the key (or message="..." if given) and ctx={"ruleId", "key", "params"}.
  • Signup.export_rules() returns the rule-format v1 dict. Field names are wire names (aliases applied); ids default to ClassName.kind.field (pass id= on a collision).
  • compare types come from the annotation: int/float → number, str → string, date → date, AwareDatetime → datetime. Naive datetime, bool and Decimal fields are rejected when the class is defined (they would not compare on the wire); pass type= to override for str fields that carry dates.

Group rules

Three builders take two or more fields. A field is blank when it is missing, None, "" or [].

from validonce import F, ValidonceModel, all_or_none, at_least_one_of, exactly_one_of


class Checkout(ValidonceModel):
    email: str | None = None
    phone: str | None = None
    card: str | None = None
    invoice: str | None = None
    street: str | None = None
    city: str | None = None
    postcode: str | None = None

    __rules__ = [
        at_least_one_of(F.email, F.phone, key="rules.contact_required"),
        exactly_one_of(F.card, F.invoice, key="rules.one_payment"),
        all_or_none(F.street, F.city, F.postcode, key="rules.address_complete"),
    ]
  • at_least_one_of fails when every field is blank; exactly_one_of fails unless exactly one is non-blank; all_or_none fails when some, but not all, are blank.
  • The error lands on the first field unless path=F.other is given. The default id is ClassName.kind.first_wire_field. params=, id=, server_only= and message= work as on the other builders.
  • Fewer than two fields, a repeated field or an unknown field raises ValidonceDefinitionError when the class is defined.

FastAPI

app = FastAPI()


@app.post("/signup")
def signup(body: Signup): ...

A failing request returns 422:

{"detail": [{
  "type": "validonce.required_when",
  "loc": ["body", "company"],
  "msg": "rules.company_required",
  "input": null,
  "ctx": {"ruleId": "Signup.required_when.company",
          "key": "rules.company_required", "params": {}}
}]}

Nested models report their own location, e.g. ["body", "bookings", 1, "end"].

/openapi.json carries the rules under components.schemas.Signup["x-validonce"]. If the model is also a response_model, FastAPI splits it into Signup-Input (has x-validonce) and Signup-Output (none); set separate_input_output_schemas=False to keep a single Signup.

Regex linter

validonce ships a regex portability linter: it finds patterns whose meaning differs between JavaScript, Python re, Rust regex and .NET (for example \w, \d, $, \Z, lookbehind, named groups). It is a port of the released C# tool and gives the same findings, spans and rewrites. The findings table lives in docs/regex-findings.json and is bundled in the wheel.

Command line

validonce lint-regex --pattern '^\w+$'
validonce lint-regex openapi.json                 # every "pattern" / patternProperties key
cat schema.json | validonce lint-regex - --format json
validonce lint-regex openapi.json --engines js,python-re --ignore VR005 --fail-on warning
validonce findings                                # list the findings table

Options: --engines (js, js-u, python-re, rust-regex, dotnet; default all), --fail-on error|warning|info|never (default error), --ignore VR005,VR014, --format text|json. Exit codes: 0 nothing at or above --fail-on, 1 findings at or above it, 2 usage error or unreadable / invalid JSON. Subtrees under example, examples, default, enum and const are skipped.

Python API

from validonce.regex import lint, utf16_span_to_py

for f in lint(r"^\w+$", engines=["js", "python-re"]):
    print(f.id, f.severity, f.rewrite)
    s, e = utf16_span_to_py(r"^\w+$", f.start, f.length)

lint(pattern, engines=None) returns PatternFinding(id, severity, start, length, title, message, rewrite, engines) sorted by position. It never raises on pattern content: an unparseable pattern gives a VR000 finding. start and length are UTF-16 code unit offsets (the same as the C# tool and JavaScript); for patterns without characters above U+FFFF they equal Python string indexes, otherwise convert with utf16_span_to_py. SAFE_REWRITE_IDS lists the findings whose rewrite is a drop-in replacement; FINDINGS is the whole table.

More

Getting started with FastAPI, the rule format and the browser runtime (npm install validonce zod): github.com/metatoy/validonce.

Copyright 2026 Metatoy LLC. Licensed under the Apache License, Version 2.0.

Metadata

Release files for validonce 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for validonce 0.5.0
File Size Uploaded Provenance Actions
validonce-0.5.0.tar.gz 121.3 kB With provenance Details for validonce-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for validonce 0.5.0
File Interpreter ABI Platform Actions
validonce-0.5.0-py3-none-any.whl Python 3 none any Details for validonce-0.5.0-py3-none-any.whl

Total release size: 155.8 kB

Release files / validonce-0.5.0.tar.gz

Download URL validonce-0.5.0.tar.gz
Size 121.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4ff081608afd12048685c1db632ed9f7758369474cff14fae1115bb151fdcb06
BLAKE2b-256 checksum
How to use checksums
cf07c883c3034d3397f3ccf206f058c2897055c324531d67fb7580688f3c458d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Release files / validonce-0.5.0-py3-none-any.whl

Download URL validonce-0.5.0-py3-none-any.whl
Size 34.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
68d3c5e7f6d7a674351d06d98d1f6a6d5b7c026a26efcd4be2f3c03ac094753e
BLAKE2b-256 checksum
How to use checksums
b5c25eaba9de42826151f93833dc3213edbec23e88a530bf47fb67133adf22ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

Security for validonce 0.5.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for validonce-0.5.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 25, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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