| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
use axum::extract::{FromRequest, Request}; |
| 14 |
use serde::de::DeserializeOwned; |
| 15 |
|
| 16 |
use crate::error::AppError; |
| 17 |
|
| 18 |
|
| 19 |
pub struct ValidatedForm<T>(pub T); |
| 20 |
|
| 21 |
impl<T, S> FromRequest<S> for ValidatedForm<T> |
| 22 |
where |
| 23 |
T: DeserializeOwned, |
| 24 |
S: Send + Sync, |
| 25 |
{ |
| 26 |
type Rejection = AppError; |
| 27 |
|
| 28 |
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> { |
| 29 |
match axum::Form::<T>::from_request(req, state).await { |
| 30 |
Ok(axum::Form(value)) => Ok(Self(value)), |
| 31 |
Err(rejection) => Err(rejection_to_validation(&rejection.body_text())), |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
pub struct ValidatedJson<T>(pub T); |
| 38 |
|
| 39 |
impl<T, S> FromRequest<S> for ValidatedJson<T> |
| 40 |
where |
| 41 |
T: DeserializeOwned, |
| 42 |
S: Send + Sync, |
| 43 |
{ |
| 44 |
type Rejection = AppError; |
| 45 |
|
| 46 |
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> { |
| 47 |
match axum::Json::<T>::from_request(req, state).await { |
| 48 |
Ok(axum::Json(value)) => Ok(Self(value)), |
| 49 |
Err(rejection) => Err(rejection_to_validation(&rejection.body_text())), |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
pub struct ValidatedHtmlForm<T>(pub T); |
| 60 |
|
| 61 |
impl<T, S> FromRequest<S> for ValidatedHtmlForm<T> |
| 62 |
where |
| 63 |
T: DeserializeOwned, |
| 64 |
S: Send + Sync, |
| 65 |
{ |
| 66 |
type Rejection = AppError; |
| 67 |
|
| 68 |
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> { |
| 69 |
match axum_extra::extract::Form::<T>::from_request(req, state).await { |
| 70 |
Ok(axum_extra::extract::Form(value)) => Ok(Self(value)), |
| 71 |
Err(rejection) => Err(rejection_to_validation(&rejection.to_string())), |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
fn rejection_to_validation(detail: &str) -> AppError { |
| 80 |
let msg = detail |
| 81 |
.strip_prefix("Failed to deserialize form body: ") |
| 82 |
.or_else(|| { |
| 83 |
detail.strip_prefix("Failed to deserialize the JSON body into the target type: ") |
| 84 |
}) |
| 85 |
.unwrap_or(detail) |
| 86 |
.trim(); |
| 87 |
let msg = if msg.is_empty() { |
| 88 |
"Please check your input and try again.".to_string() |
| 89 |
} else { |
| 90 |
msg.to_string() |
| 91 |
}; |
| 92 |
AppError::validation(msg) |
| 93 |
} |
| 94 |
|
| 95 |
#[cfg(test)] |
| 96 |
mod tests { |
| 97 |
use super::*; |
| 98 |
|
| 99 |
#[test] |
| 100 |
fn strips_form_prefix() { |
| 101 |
let e = rejection_to_validation("Failed to deserialize form body: slug must be lowercase"); |
| 102 |
assert_eq!(e.user_message(), "slug must be lowercase"); |
| 103 |
} |
| 104 |
|
| 105 |
#[test] |
| 106 |
fn strips_json_prefix() { |
| 107 |
let e = rejection_to_validation( |
| 108 |
"Failed to deserialize the JSON body into the target type: price too high", |
| 109 |
); |
| 110 |
assert_eq!(e.user_message(), "price too high"); |
| 111 |
} |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn falls_back_when_empty() { |
| 115 |
let e = rejection_to_validation(""); |
| 116 |
assert_eq!(e.user_message(), "Please check your input and try again."); |
| 117 |
} |
| 118 |
} |
| 119 |
|