pub struct Valid<T>(pub T);Expand description
Validated extractor wrapper.
Wraps any extractor and runs validation after extraction.
Use this with types that implement the Validate trait.
§Error Responses
- If extraction fails, returns the inner extractor’s error response
- If validation fails, returns 422 Unprocessable Entity with validation errors
§Example
ⓘ
use fastapi_core::extract::{Json, Valid};
use fastapi_core::validation::Validate;
use fastapi_macros::Validate;
use serde::Deserialize;
#[derive(Deserialize, Validate)]
struct CreateUser {
#[validate(email)]
email: String,
#[validate(length(min = 3, max = 50))]
username: String,
}
async fn create_user(Valid(Json(user)): Valid<Json<CreateUser>>) -> impl IntoResponse {
format!("Created user: {}", user.username)
}Tuple Fields§
§0: TImplementations§
Source§impl<T> Valid<T>
impl<T> Valid<T>
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Unwrap the inner value.
Trait Implementations§
Source§impl<T> FromRequest for Valid<T>
impl<T> FromRequest for Valid<T>
Source§type Error = ValidExtractError<<T as FromRequest>::Error>
type Error = ValidExtractError<<T as FromRequest>::Error>
Error type when extraction fails.
Source§async fn from_request(
ctx: &RequestContext,
req: &mut Request,
) -> Result<Self, Self::Error>
async fn from_request( ctx: &RequestContext, req: &mut Request, ) -> Result<Self, Self::Error>
Extract a value from the request. Read more
impl<T: Copy> Copy for Valid<T>
Auto Trait Implementations§
impl<T> Freeze for Valid<T>where
T: Freeze,
impl<T> RefUnwindSafe for Valid<T>where
T: RefUnwindSafe,
impl<T> Send for Valid<T>where
T: Send,
impl<T> Sync for Valid<T>where
T: Sync,
impl<T> Unpin for Valid<T>where
T: Unpin,
impl<T> UnwindSafe for Valid<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: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).