Skip to main content

AsyncRule

Trait AsyncRule 

Source
pub trait AsyncRule: Send + Sync {
    // Required methods
    fn validate<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        field: &'life1 str,
        value: &'life2 Value,
        data: &'life3 Value,
    ) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait;
    fn name(&self) -> &'static str;
}
Expand description

An async validation rule for DB-backed or I/O-bound checks.

The async counterpart to crate::validation::Rule. Implement this for rules that require async I/O (e.g. DB lookups). Stored as Box<dyn AsyncRule>, mirroring Box<dyn Rule>.

#[async_trait] is required because stable Rust async fn in traits is not dyn-compatible — Box<dyn AsyncRule> needs the async_trait transform.

§Error semantics (D-12)

validate returns Err(message) ONLY for field-level validation failures. I/O / infrastructure errors (e.g. a DB connection error) MUST NOT be returned as a validation message. They are signalled to the validator with a message prefixed __infra_error__: so crate::validation::AsyncValidator can re-raise them as a framework error (HTTP 500) instead of a field error.

§Safety

Send + Sync are required: boxed trait objects are held across .await points and shared across async request tasks.

Required Methods§

Source

fn validate<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, field: &'life1 str, value: &'life2 Value, data: &'life3 Value, ) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Validate the field value. Ok(()) on pass; Err(message) on a field-level validation failure. See trait docs for the __infra_error__: sentinel used for infrastructure failures.

Source

fn name(&self) -> &'static str

Rule name, used for custom-message lookup (e.g. "unique").

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§