pub struct ValidatedJson<T>(pub T);Expand description
An Axum extractor that deserializes a JSON request body and validates it with
validator.
On success it yields ValidatedJson(value). On failure it short-circuits the handler
with a (StatusCode, Json<ApiError>) response:
- malformed JSON ->
400 Bad Request, codeINVALID_JSON - well-formed JSON of the wrong shape ->
422 Unprocessable Entity, codeINVALID_BODY - missing or incorrect
Content-Type->415 Unsupported Media Type, codeUNSUPPORTED_MEDIA_TYPE - validation failure ->
422 Unprocessable Entity, codeVALIDATION_ERRORwith field-leveldetails(seeApiError’sFrom<validator::ValidationErrors>impl)
Requires the validator feature.
§Example
use axum_api_kit::ValidatedJson;
use serde::Deserialize;
use validator::Validate;
#[derive(Deserialize, Validate)]
struct CreateUser {
#[validate(length(min = 1, max = 100))]
name: String,
#[validate(email)]
email: String,
}
// The body is deserialized and validated before the handler body runs.
async fn create_user(ValidatedJson(user): ValidatedJson<CreateUser>) {
let _ = (user.name, user.email);
}Tuple Fields§
§0: TTrait Implementations§
Source§impl<T: Clone> Clone for ValidatedJson<T>
impl<T: Clone> Clone for ValidatedJson<T>
Source§fn clone(&self) -> ValidatedJson<T>
fn clone(&self) -> ValidatedJson<T>
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl<T: Debug> Debug for ValidatedJson<T>
impl<T: Debug> Debug for ValidatedJson<T>
Source§impl<T, S> FromRequest<S> for ValidatedJson<T>
impl<T, S> FromRequest<S> for ValidatedJson<T>
Auto Trait Implementations§
impl<T> Freeze for ValidatedJson<T>where
T: Freeze,
impl<T> RefUnwindSafe for ValidatedJson<T>where
T: RefUnwindSafe,
impl<T> Send for ValidatedJson<T>where
T: Send,
impl<T> Sync for ValidatedJson<T>where
T: Sync,
impl<T> Unpin for ValidatedJson<T>where
T: Unpin,
impl<T> UnsafeUnpin for ValidatedJson<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for ValidatedJson<T>where
T: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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 more