Skip to main content

Validate

Derive Macro Validate 

Source
#[derive(Validate)]
{
    // Attributes available to this derive:
    #[schema]
    #[field]
    #[serde]
    #[model_config]
}
Expand description

Derive siderite::validation::Validate.

Serde stays the single source of truth for wire names, defaults and skipping; derive(Schema), derive(Validate) and the generated Dump share one attribute model, so key names and required-ness cannot drift.

§#[model_config(...)]

strict, extra = "forbid" | "ignore" | "allow", str_strip_whitespace, str_to_lower, str_to_upper, populate_by_name (the Rust field name is accepted as an input key too) and hooks (see below). Flags accept flag and flag = true. Serde’s deny_unknown_fields implies extra = "forbid" unless extra is given. As with deny_unknown_fields, the key of a #[serde(skip_deserializing)] (or skip) field is not an input, so under extra = "forbid" sending it is extra_forbidden.

§#[field(...)]

Constraints: min_length, max_length, pattern (or regex, checked at compile time), email, gt, ge, lt, le, multiple_of (numeric literals). Naming: alias (must equal the serde key; use #[serde(rename)] to rename), validation_alias (extra accepted input key), serialization_alias (must equal the serde key: there is one schema per type, unlike Pydantic). Defaults: default = <expr> / default_factory = path document the default and drive exclude_defaults; the field must also have a serde default (#[serde(default)], #[serde(default = "path")], or a container default), which stays the source of truth for deserialization. Annotations: title, description, examples(..), url, max_digits/decimal_places (documented as x- schema extensions, not enforced). Behaviour: exclude (skipped by Dump, input still accepted), strict (strict coercion for this field only) and validator = path (repeatable reusable after-validator fn(&FieldTy) -> Result<(), FieldError>). Unknown keys are compile errors listing the valid ones.

§Supported shapes

  • Structs with named fields: full validation.
  • Newtype structs delegate to the inner type.
  • Enums with only unit variants (default representation) check that the input is one of the serde names (error code enum).
  • Other enums and tuple/unit structs implement Validate without validating anything themselves.

§Hooks and generics

Hooks defined with model_hooks are found automatically for concrete types. For generic types the derive cannot detect a ModelHooks impl, so it emits no hook calls unless the type says #[model_config(hooks)], which calls <Self as ModelHooks> directly (an impl is then required, even if empty). Type parameters get a Validate bound.