AsyncValidate

Trait AsyncValidate 

Source
pub trait AsyncValidate:
    Validate
    + Send
    + Sync {
    // Required method
    fn validate_async<'life0, 'life1, 'async_trait>(
        &'life0 self,
        ctx: &'life1 ValidationContext,
    ) -> Pin<Box<dyn Future<Output = Result<(), ValidationErrors>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided methods
    fn validate_full<'life0, 'life1, 'async_trait>(
        &'life0 self,
        ctx: &'life1 ValidationContext,
    ) -> Pin<Box<dyn Future<Output = Result<(), ValidationErrors>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
    fn validated_async<'life0, 'async_trait>(
        self,
        ctx: &'life0 ValidationContext,
    ) -> Pin<Box<dyn Future<Output = Result<Self, ValidationErrors>> + Send + 'async_trait>>
       where Self: Sized + 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

Trait for asynchronous validation of a struct.

Use this trait when validation requires async operations like database checks or API calls.

§Example

use rustapi_validate::v2::prelude::*;

struct CreateUser {
    email: String,
}

#[async_trait]
impl AsyncValidate for CreateUser {
    async fn validate_async(&self, ctx: &ValidationContext) -> Result<(), ValidationErrors> {
        let mut errors = ValidationErrors::new();
         
        // Check email uniqueness in database
        if let Some(db) = ctx.database() {
            let rule = AsyncUniqueRule::new("users", "email");
            if let Err(e) = rule.validate_async(&self.email, ctx).await {
                errors.add("email", e);
            }
        }
         
        errors.into_result()
    }
}

Required Methods§

Source

fn validate_async<'life0, 'life1, 'async_trait>( &'life0 self, ctx: &'life1 ValidationContext, ) -> Pin<Box<dyn Future<Output = Result<(), ValidationErrors>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Validate the struct asynchronously.

This method is called after validate() and can perform async operations like database queries or external API calls.

Provided Methods§

Source

fn validate_full<'life0, 'life1, 'async_trait>( &'life0 self, ctx: &'life1 ValidationContext, ) -> Pin<Box<dyn Future<Output = Result<(), ValidationErrors>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Perform full validation (sync + async).

Source

fn validated_async<'life0, 'async_trait>( self, ctx: &'life0 ValidationContext, ) -> Pin<Box<dyn Future<Output = Result<Self, ValidationErrors>> + Send + 'async_trait>>
where Self: Sized + 'async_trait, 'life0: 'async_trait,

Validate and return the struct if valid (async version).

Implementors§