Skip to content

How to implement FromRequest for pub struct Validated<T>(pub Json<T>) #2793

Answered by zishon
zishon asked this question in Q&A
Discussion options

You must be logged in to vote

maybe something like this

impl IntoResponse for RequestValidateErrors {
    //some implements
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Validated<T>(pub T);

impl<S, B, T> FromRequest<B> for Validated<T>
    where
        B: Send + 'static,
        T: DeserializeOwned + Debug + Validate + FromRequest<S, B>,
        S: Send + Sync,
{
    type Rejection = RequestValidateErrors;

    async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> {
        let bytes = axum::body::to_bytes(req.into_body(), 100000).await.unwrap();

        let value = serde_json::from_slice::<T>(&bytes).unwrap();
        
        validate_res = value.validate();
        //proc…

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@zishon
Comment options

@mladedav
Comment options

@zishon
Comment options

Answer selected by zishon
@jplatte
Comment options

@jplatte
Comment options

@zishon
Comment options

@jplatte
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants