Skip to main content

Module validation

Module validation 

Source
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§

ErrorBag
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 localized 422 body.
FieldRules
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::Unique check ignores the edited row on update, and Rule::RequiredOn fires 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-backed Rule::Unique probe for each field that carries it, has a value, and passed its cheap rules. edited_id is the row being updated (ignored by the probe); pass None on create.
validate_fields
Runs the cheap, non-database rules over data for mode, returning the error bag. Rule::Unique needs the database and is ignored here; use validate to include it. Exposed on its own for callers that want a DB-free check (for example an API validating a payload before any query).