Skip to main content

Validate

Derive Macro Validate 

Source
#[derive(Validate)]
{
    // Attributes available to this derive:
    #[validate]
}
Expand description

Derives the modo::validate::Validate trait for a named-field struct.

Annotate fields with #[validate(...)] to declare one or more rules. Available rules:

  • required — field must not be None (for Option) or empty (for String)
  • min_length = N / max_length = N — minimum/maximum character count for strings
  • email — basic email format check
  • min = V / max = V — numeric range for comparable types
  • custom = "path::to::fn" — call a fn(&T) -> Result<(), String> function

Each rule accepts an optional (message = "...") override. A field-level message = "..." key acts as a fallback for all rules on that field.

validate() returns Ok(()) or Err(modo::Error) containing all collected error messages keyed by field name.