Skip to main content

Validate

Derive Macro Validate 

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

Derive validation for a struct.

§Validation Attributes

  • #[validate(length(min = 1, max = 100))] - String length
  • #[validate(range(min = 0.0, max = 1.0))] - Numeric range
  • #[validate(email)] - Email format
  • #[validate(regex = "pattern")] - Regex pattern

§Example

#[derive(Validate)]
struct CreateUser {
    #[validate(length(min = 1, max = 50))]
    name: String,
    #[validate(email)]
    email: String,
}