mirror of
https://github.com/DefectingCat/phthonus
synced 2025-07-15 16:41:32 +00:00
29 lines
729 B
Rust
29 lines
729 B
Rust
use axum::{
|
|
async_trait,
|
|
extract::{rejection::FormRejection, FromRequest, Request},
|
|
Form, Json,
|
|
};
|
|
use serde::de::DeserializeOwned;
|
|
use validator::Validate;
|
|
|
|
use crate::error::{AppError, AppResult};
|
|
|
|
#[derive(Debug, Clone, Copy, Default)]
|
|
pub struct ValidatedJson<T>(pub T);
|
|
|
|
#[async_trait]
|
|
impl<T, S> FromRequest<S> for ValidatedJson<T>
|
|
where
|
|
T: DeserializeOwned + Validate,
|
|
S: Send + Sync,
|
|
Form<T>: FromRequest<S, Rejection = FormRejection>,
|
|
{
|
|
type Rejection = AppError;
|
|
|
|
async fn from_request(req: Request, state: &S) -> AppResult<Self, Self::Rejection> {
|
|
let Json(value) = Json::<T>::from_request(req, state).await?;
|
|
value.validate()?;
|
|
Ok(ValidatedJson(value))
|
|
}
|
|
}
|