Expand description
One validation engine for admin forms and the API.
Typed Rules run over a field’s submitted value into an ErrorBag of
locale-free Text messages: the admin form reads it field by field and
localizes each inline, the API localizes it into a 422 body. Cheap rules
(required, length, format) run without touching the database; the one DB-backed
rule, Rule::Unique, runs a single EXISTS-style probe and only for a field
that already passed its cheap rules, so a blank or malformed value is never also
probed.
The engine is bespoke because Laterite’s rules are runtime and
descriptor-driven (a FormConfig or YAML over a HashMap), which the
derive-macro crates (validator, garde) do not fit, and because Unique
is inherently database-specific. The one genuinely-standard primitive, email
shape, is delegated to the RFC-compliant email_address crate.
Structs§
- Error
Bag - Per-field validation messages, each a locale-free
Text. Empty means valid. Whoever renders the bag localizes each message at its sink: the admin form through the request translator, the API into a localized422body. - Field
Rules - The rules for one field, with the label used in its messages.
Enums§
- Mode
- Whether a submission creates a new record or updates an existing one. A
Rule::Uniquecheck ignores the edited row on update, andRule::RequiredOnfires for one mode only. - Rule
- A single rule applied to one field’s submitted value. Serde-serialisable so
a descriptor (later YAML) can author rules by name (
required,email,max_length, …).
Functions§
- validate
- Runs
validate_fields, then the DB-backedRule::Uniqueprobe for each field that carries it, has a value, and passed its cheap rules.edited_idis the row being updated (ignored by the probe); passNoneon create. - validate_
fields - Runs the cheap, non-database rules over
dataformode, returning the error bag.Rule::Uniqueneeds the database and is ignored here; usevalidateto include it. Exposed on its own for callers that want a DB-free check (for example an API validating a payload before any query).