pub struct ValidationExtractor<T>(pub T);Expand description
A wrapper type that validates incoming JSON request bodies against a set of validation rules.
This extractor combines JSON deserialization with automatic validation using
the validator crate. It’s designed to be used as a request extractor in
Axum route handlers.
§Type Parameters
T- The type to deserialize the JSON into. Must implement bothDeserializeOwnedandValidate.
§Examples
use serde::Deserialize;
use validator::Validate;
#[derive(Deserialize, Validate)]
struct CreateUser {
#[validate(length(min = 3, max = 50))]
username: String,
#[validate(email)]
email: String,
}
async fn create_user(
ValidationExtractor(payload): ValidationExtractor<CreateUser>
) -> impl Response {
// payload is guaranteed to be valid here
// ...
}§Validation Flow
- The incoming request body is first deserialized as JSON into type
T - The deserialized value is validated using its
Validateimplementation - If both steps succeed, the value is wrapped in
ValidationExtractorand passed to the handler - If either step fails, an error is returned and the handler is not called
Tuple Fields§
§0: TTrait Implementations§
source§impl<T, S> FromRequest<S> for ValidationExtractor<T>where
T: DeserializeOwned + Validate,
S: Send + Sync,
Json<T>: FromRequest<S, Rejection = JsonRejection>,
impl<T, S> FromRequest<S> for ValidationExtractor<T>where
T: DeserializeOwned + Validate,
S: Send + Sync,
Json<T>: FromRequest<S, Rejection = JsonRejection>,
Implementation of FromRequest for ValidationExtractor<T>.
This implementation enables automatic extraction and validation of JSON request bodies in Axum route handlers.
§Type Parameters
T- The type to deserialize and validate (must implementDeserializeOwnedandValidate)S- The state type for the Axum application
§Errors
Returns an error in the following cases:
- JSON deserialization fails
- Validation rules are not satisfied
Auto Trait Implementations§
impl<T> Freeze for ValidationExtractor<T>where
T: Freeze,
impl<T> RefUnwindSafe for ValidationExtractor<T>where
T: RefUnwindSafe,
impl<T> Send for ValidationExtractor<T>where
T: Send,
impl<T> Sync for ValidationExtractor<T>where
T: Sync,
impl<T> Unpin for ValidationExtractor<T>where
T: Unpin,
impl<T> UnwindSafe for ValidationExtractor<T>where
T: UnwindSafe,
Blanket Implementations§
§impl<T> AnySync for T
impl<T> AnySync for T
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
The archived version of the pointer metadata for this type.
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Converts some archived metadata to the pointer metadata for itself.
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<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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 moreCreates a shared type from an unshared type.
§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Returns the layout of the type.