Skip to main content

Validate

Derive Macro Validate 

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

The main procedural macro function for #[derive(Validate)].

This function is invoked by the Rust compiler when #[derive(Validate)] is encountered. It takes the TokenStream of the struct definition as input and returns a TokenStream containing the generated impl Validate for ... block.

§Generated Traits

  • impl Validate for T - Simple validation without context
  • impl ValidateArgs<'_> for T - Validation with optional context

§Steps

  1. Parses the input TokenStream into a syn::DeriveInput
  2. Uses ValidationData::from_derive_input (powered by darling) to parse attributes into the ValidationData struct
  3. Extracts necessary information like struct identifier, generics, and parsed field data
  4. Generates use statements for validator traits
  5. Generates code for struct-level schema validations
  6. Iterates over fields, generating field-specific validation logic
  7. Assembles the final impl ValidateArgs for ... and optionally impl Validate for ... blocks