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)). OneValidationErrorlists every failing rule. - Each error sits on its target field, with
type="validonce.<kind>",msg= the key (ormessage="..."if given) andctx={"ruleId", "key", "params"}. Signup.export_rules()returns the rule-format v1 dict. Field names are wire names (aliases applied); ids default toClassName.kind.field(passid=on a collision).comparetypes come from the annotation:int/float→ number,str→ string,date→ date,AwareDatetime→ datetime. Naivedatetime,boolandDecimalfields are rejected when the class is defined (they would not compare on the wire); passtype=to override forstrfields 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_offails when every field is blank;exactly_one_offails unless exactly one is non-blank;all_or_nonefails when some, but not all, are blank.- The error lands on the first field unless
path=F.otheris given. The default id isClassName.kind.first_wire_field.params=,id=,server_only=andmessage=work as on the other builders. - Fewer than two fields, a repeated field or an unknown field raises
ValidonceDefinitionErrorwhen 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.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| validonce-0.3.1.tar.gz | 120.5 kB | With provenance | Details for validonce-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| validonce-0.3.1-py3-none-any.whl | Python 3 | none | any | Details for validonce-0.3.1-py3-none-any.whl |
Total release size: 154.9 kB
Release files / validonce-0.3.1.tar.gz
| Download URL | validonce-0.3.1.tar.gz |
|---|---|
| Size | 120.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e7d70b2672cda18066a9d78f553a84ca72396b7657391e95e24e0a77deb6d029
|
|
BLAKE2b-256 checksum How to use checksums |
967d7705a5c55379ffa24a0040b8492d22d5e84e6c114c1eeede0c1a4edd628a
|
| 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 24, 2026.
Transparency logRelease files / validonce-0.3.1-py3-none-any.whl
| Download URL | validonce-0.3.1-py3-none-any.whl |
|---|---|
| Size | 34.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2dfbc2e764e26af09c4ca47dcf0392fc1afdafc72ca481c4601171a8091f318c
|
|
BLAKE2b-256 checksum How to use checksums |
1b9fdced3d4f19e4000800b1a869c5a4b841e2f94e56ea16b3e7fe01659e78f7
|
| 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 24, 2026.
Transparency logSecurity for validonce 0.3.1
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.
Attestation for validonce-0.3.1.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 24, 2026.
Transparency logA 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.