pub struct AsyncValidator<'a> { /* private fields */ }Expand description
Async request validator.
Mirrors crate::validation::Validator ergonomics while adding support for
Box<dyn AsyncRule> rules (e.g. DB uniqueness checks). Sync rules run
first; async rules run only on fields with no sync error (fail-fast, D-03).
§Example
use ferro_rs::{AsyncValidator, AsyncValidationError, unique};
use ferro_rs::validation::rules::*;
use ferro_rs::rules;
let data = req.input::<serde_json::Value>().await?;
match AsyncValidator::new(&data)
.rules("slug", rules![required(), string()])
.async_rule("slug", unique("articles", "slug"))
.validate_async()
.await
{
Ok(()) => {}
Err(AsyncValidationError::Validation(e)) => {
return Err(e.with_old_input(&data).into_action_error("/articles/new"));
}
Err(AsyncValidationError::Infra(fe)) => {
return Err(fe.into());
}
}Implementations§
Source§impl<'a> AsyncValidator<'a>
impl<'a> AsyncValidator<'a>
Sourcepub fn rule<R: Rule + 'static>(self, field: impl Into<String>, rule: R) -> Self
pub fn rule<R: Rule + 'static>(self, field: impl Into<String>, rule: R) -> Self
Add a single sync validation rule for a field.
Sourcepub fn async_rule<R: AsyncRule + 'static>(
self,
field: impl Into<String>,
rule: R,
) -> Self
pub fn async_rule<R: AsyncRule + 'static>( self, field: impl Into<String>, rule: R, ) -> Self
Add a single async validation rule for a field.
Async rules run only after all sync rules pass for the field (D-03).
Sourcepub fn messages(self, messages: HashMap<String, String>) -> Self
pub fn messages(self, messages: HashMap<String, String>) -> Self
Set custom messages from a map.
Sourcepub fn attributes(self, attributes: HashMap<String, String>) -> Self
pub fn attributes(self, attributes: HashMap<String, String>) -> Self
Set custom attributes from a map.
Sourcepub async fn validate_async(self) -> Result<(), AsyncValidationError>
pub async fn validate_async(self) -> Result<(), AsyncValidationError>
Run async validation. Returns:
Ok(())— all rules pass.Err(AsyncValidationError::Validation(e))— field-level failures.Err(AsyncValidationError::Infra(e))— DB/infra failure (handler → 500).
§Execution order (D-03)
Phase 1: all sync rules run across all fields. Phase 2: async rules run only on fields with no sync error — no DB query is issued for an already-failed field.
§Infra sentinel (D-12)
An async rule that returns Err(msg) where msg starts with
__infra_error__: is treated as an infrastructure failure, not a
field error. The stripped message is wrapped in
AsyncValidationError::Infra(FrameworkError::database(...)) and
returned immediately.
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for AsyncValidator<'a>
impl<'a> !UnwindSafe for AsyncValidator<'a>
impl<'a> Freeze for AsyncValidator<'a>
impl<'a> Send for AsyncValidator<'a>
impl<'a> Sync for AsyncValidator<'a>
impl<'a> Unpin for AsyncValidator<'a>
impl<'a> UnsafeUnpin for AsyncValidator<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more