pub enum ValidationError {
Invalid(InvalidInput),
Failed(Error),
}Expand description
The error from validating input: field errors, or a failure that stopped validation.
Request::validate and Rules::validate
return this error, so a handler can match the field errors without downcasting. When a handler
that returns laravel::Result propagates it with ?, the response is the usual
validation response for field errors, or the usual error response for a failure.
use laravel::prelude::*;
use laravel::validation::{Validate, ValidationError};
#[derive(serde::Deserialize, Validate)]
#[validate(anyone)]
struct Input {
#[validate(required)]
name: String,
}
async fn store(request: Request) -> laravel::Result<Response> {
let input = match request.validate::<Input>().await {
Ok(input) => input,
Err(ValidationError::Invalid(errors)) => {
return Ok(Response::new((
StatusCode::UNPROCESSABLE_ENTITY,
view("users.create", data! { title => "Create a user" })
.with_errors(&errors)
.with_input(&request),
)));
}
Err(ValidationError::Failed(error)) => return Err(error),
};
Ok(Response::new(format!("Hello, {}", input.name)))
}Variants§
Invalid(InvalidInput)
One or more fields failed their rules or didn’t decode into the field’s type.
Failed(Error)
Validation stopped before it finished. This holds a 403 when authorize returns false,
or the error of a check that couldn’t run, such as when the database is unavailable.
Trait Implementations§
Source§impl Debug for ValidationError
impl Debug for ValidationError
Source§impl Display for ValidationError
impl Display for ValidationError
Source§impl From<ValidationError> for Error
impl From<ValidationError> for Error
Source§fn from(error: ValidationError) -> Error
fn from(error: ValidationError) -> Error
Converts to this type from the input type.
Auto Trait Implementations§
impl Forget for ValidationError
impl Freeze for ValidationErrorwhere
*const u8 is !null: Freeze,
PhantomData<usize>: Freeze,
PhantomData<Bucket<String, Vec<String>>>: Freeze,
PhantomData<u8>: Freeze,
*const ArcInner<Inner> is !null: Freeze,
PhantomData<ArcInner<Inner>>: Freeze,
impl Move for ValidationError
impl RefUnwindSafe for ValidationErrorwhere
Atomic<usize>: RefUnwindSafe,
Error: RefUnwindSafe,
NonZero<u16>: RefUnwindSafe,
Atomic<*mut ()>: RefUnwindSafe,
impl Send for ValidationError
impl Sync for ValidationError
impl Unpin for ValidationError
impl UnsafeUnpin for ValidationErrorwhere
*const u8 is !null: UnsafeUnpin,
PhantomData<usize>: UnsafeUnpin,
PhantomData<Bucket<String, Vec<String>>>: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
*const ArcInner<Inner> is !null: UnsafeUnpin,
PhantomData<ArcInner<Inner>>: UnsafeUnpin,
impl UnwindSafe for ValidationError
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
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<K> ForeignKey<K> for K
impl<K> ForeignKey<K> for K
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
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> ⓘ
Converts
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> ⓘ
Converts
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 moreSource§impl<T> IntoMarkdown for Twhere
T: Display,
impl<T> IntoMarkdown for Twhere
T: Display,
Creates a shared type from an unshared type.