Skip to content

Custom extractor to validate query string. #2081

Answered by davidpdrsn
goni098 asked this question in Q&A
Discussion options

You must be logged in to vote

The issue is actually with the Query<T>: FromRequest<S, B> bound. You'll wanna use Query<T>: FromRequestParts<S> instead since Query doesn't need the request body.

This works for me

use axum::{
    async_trait,
    extract::{FromRequest, FromRequestParts, Query},
    http, response,
};
use serde::{Deserialize, Serialize};
use validator::Validate;

pub struct Validated<T>(pub T);

#[async_trait]
impl<S, B, T> FromRequest<S, B> for Validated<T>
where
    S: Send + Sync,
    B: Send + 'static,
    T: Validate,
    Query<T>: FromRequestParts<S>,
{
    type Rejection = (http::StatusCode, String);

    async fn from_request(req: http::Request<B>, state: &S) -> Result<Self, Self::Rejection> {

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@goni098
Comment options

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