Conflicts when making a ValidatorRequest in Axum Rust

I'm trying to create a Request Validator in Rust, but I keep running into some conflicts in the Axum library

use async_trait::async_trait;
use axum::extract::{rejection::JsonRejection, FromRequest};
use axum::{body::HttpBody, http::Request, BoxError, Json};
use axum::response::{IntoResponse, Response};
use serde::de::DeserializeOwned;
use thiserror::Error;
use validator::Validate;
use crate::v3::config::response::ApiErrorResponse;

#[derive(Debug, Error)]
pub enum RequestError {
    #[error(transparent)]
    ValidationError(#[from] validator::ValidationErrors),
    #[error(transparent)]
    JsonRejection(#[from] JsonRejection),
}
#[derive(Debug, Clone, Copy, Default)]
pub struct ValidatedRequest<T>(pub T);

#[async_trait]
impl<T, S, B> FromRequest<S, B> for ValidatedRequest<T>
    where
        T: DeserializeOwned + Validate,
        S: Send + Sync,
        B: HttpBody + Send + 'static,
        B::Data: Send,
        B::Error: Into<BoxError>,
{
    type Rejection = RequestError;

    async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
        let Json(value) = Json::<T>::from_request(req, state).await?;
        value.validate()?;
        Ok(ValidatedRequest(value))
    }
}

impl IntoResponse for RequestError {
    fn into_response(self) -> Response {
        match self {
            RequestError::ValidationError(_) => {
                ApiErrorResponse::send(400, Some(self.to_string().replace('\n', ", ")))
            }
            RequestError::JsonRejection(_) => ApiErrorResponse::send(400, Some(self.to_string())),
        }
    }
}
impl<T, S, B> FromRequest<S, B> for ValidatedRequest<T> -> Here is the error
error[E0119]: conflicting implementations of trait `FromRequest<_, axum_core::extract::private::ViaParts>` for type `ValidatedRequest<_>`
  --> src/v3/error/request.rs:21:1
   |
21 | / impl<T, S, B> FromRequest<S, B> for ValidatedRequest<T>
22 | |     where
23 | |         T: DeserializeOwned + Validate,
24 | |         S: Send + Sync,
25 | |         B: HttpBody + Send + 'static,
26 | |         B::Data: Send,
27 | |         B::Error: Into<BoxError>,
   | |_________________________________^
   |
   = note: conflicting implementation in crate `axum_core`:
           - impl<S, T> FromRequest<S, axum_core::extract::private::ViaParts> for T
             where S: Send, S: Sync, T: FromRequestParts<S>;
   = note: downstream crates may implement trait `axum::extract::FromRequestParts<_>` for type `v3::error::request::ValidatedRequest<_>`
   = note: upstream crates may add a new impl of trait `axum::body::HttpBody` for type `axum_core::extract::private::ViaParts` in future versions

I can't find any resources about this problem on the net. How could I solve the problem? Is there a way?

I should be able to validate a request with a body from the user

Also cross-posted on Stack Overflow.

When asking questions, please be so kind and indicate cross-posts, so there isn’t a chance that people answering your question would need to come up with answers that were already given elsewhere. It’s the least you can do to value everyone’s time. Furthermore, seeing replies others have already given can allow someone to write better answers themself, it allows elaborating on or correcting things others have said, and it ensures all the context from things like questions directed at the OP, etc… is present.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.