|
30 | 30 |
|
31 | 31 | use ::axum::{ |
32 | 32 | Json, |
33 | | - extract::{FromRequest, Request}, |
34 | | - http::{StatusCode, header::CONTENT_TYPE}, |
| 33 | + extract::{FromRequest, FromRequestParts, Request}, |
| 34 | + http::{HeaderValue, StatusCode, header::CONTENT_TYPE, request::Parts}, |
35 | 35 | response::{IntoResponse, Response}, |
36 | 36 | }; |
37 | 37 | use ::garde::Validate; |
@@ -185,65 +185,44 @@ impl<C, T> DerefMut for ValidatedWith<C, T> { |
185 | 185 | } |
186 | 186 | } |
187 | 187 |
|
188 | | -impl<U> ValidatePayload for Json<U> |
| 188 | +impl<T> ValidatePayload for T |
189 | 189 | where |
190 | | - U: Validate<Context = ()>, |
| 190 | + T: ValidatePayloadWith<()>, |
191 | 191 | { |
192 | | - type Inner = U; |
193 | | - fn payload(&self) -> &U { |
194 | | - &self.0 |
195 | | - } |
196 | | -} |
| 192 | + type Inner = <T as ValidatePayloadWith<()>>::Inner; |
197 | 193 |
|
198 | | -impl<U> ValidatePayload for ::axum::Form<U> |
199 | | -where |
200 | | - U: Validate<Context = ()>, |
201 | | -{ |
202 | | - type Inner = U; |
203 | | - fn payload(&self) -> &U { |
204 | | - &self.0 |
| 194 | + fn payload(&self) -> &Self::Inner { |
| 195 | + <Self as ValidatePayloadWith<()>>::payload(self) |
205 | 196 | } |
206 | 197 | } |
207 | 198 |
|
208 | | -impl<U> ValidatePayload for ::axum::extract::Query<U> |
209 | | -where |
210 | | - U: Validate<Context = ()>, |
211 | | -{ |
212 | | - type Inner = U; |
213 | | - fn payload(&self) -> &U { |
214 | | - &self.0 |
215 | | - } |
216 | | -} |
217 | | - |
218 | | -impl<U> ValidatePayload for ::axum::extract::Path<U> |
| 199 | +impl<S, T> FromRequest<S> for Validated<T> |
219 | 200 | where |
220 | | - U: Validate<Context = ()>, |
| 201 | + S: Send + Sync, |
| 202 | + T: FromRequest<S> + ValidatePayload + Send, |
221 | 203 | { |
222 | | - type Inner = U; |
223 | | - fn payload(&self) -> &U { |
224 | | - &self.0 |
225 | | - } |
226 | | -} |
| 204 | + type Rejection = Response; |
227 | 205 |
|
228 | | -impl<U> ValidatePayload for crate::multipart::TypedMultipart<U> |
229 | | -where |
230 | | - U: Validate<Context = ()>, |
231 | | -{ |
232 | | - type Inner = U; |
233 | | - fn payload(&self) -> &U { |
234 | | - &self.0 |
| 206 | + async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> { |
| 207 | + let extracted = T::from_request(req, state) |
| 208 | + .await |
| 209 | + .map_err(IntoResponse::into_response)?; |
| 210 | + match extracted.payload().validate() { |
| 211 | + Ok(()) => Ok(Self(extracted)), |
| 212 | + Err(report) => Err(build_validation_response(&report)), |
| 213 | + } |
235 | 214 | } |
236 | 215 | } |
237 | 216 |
|
238 | | -impl<S, T> FromRequest<S> for Validated<T> |
| 217 | +impl<S, T> FromRequestParts<S> for Validated<T> |
239 | 218 | where |
240 | 219 | S: Send + Sync, |
241 | | - T: FromRequest<S> + ValidatePayload + Send, |
| 220 | + T: FromRequestParts<S> + ValidatePayload + Send, |
242 | 221 | { |
243 | 222 | type Rejection = Response; |
244 | 223 |
|
245 | | - async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> { |
246 | | - let extracted = T::from_request(req, state) |
| 224 | + async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { |
| 225 | + let extracted = T::from_request_parts(parts, state) |
247 | 226 | .await |
248 | 227 | .map_err(IntoResponse::into_response)?; |
249 | 228 | match extracted.payload().validate() { |
@@ -275,6 +254,28 @@ where |
275 | 254 | } |
276 | 255 | } |
277 | 256 |
|
| 257 | +impl<S, C, T> FromRequestParts<S> for ValidatedWith<C, T> |
| 258 | +where |
| 259 | + S: Send + Sync + ValidationContext<C>, |
| 260 | + C: Send + Sync + 'static, |
| 261 | + T: FromRequestParts<S> + ValidatePayloadWith<C> + Send, |
| 262 | +{ |
| 263 | + type Rejection = Response; |
| 264 | + |
| 265 | + async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { |
| 266 | + let extracted = T::from_request_parts(parts, state) |
| 267 | + .await |
| 268 | + .map_err(IntoResponse::into_response)?; |
| 269 | + match extracted |
| 270 | + .payload() |
| 271 | + .validate_with(state.validation_context()) |
| 272 | + { |
| 273 | + Ok(()) => Ok(Self::new(extracted)), |
| 274 | + Err(report) => Err(build_validation_response(&report)), |
| 275 | + } |
| 276 | + } |
| 277 | +} |
| 278 | + |
278 | 279 | /// Build the canonical `422 Unprocessable Entity` response from a |
279 | 280 | /// [`garde::Report`]. |
280 | 281 | /// |
@@ -374,10 +375,10 @@ fn build_validation_response(report: &::garde::Report) -> Response { |
374 | 375 | br#"{"errors":[{"message":"request validation failed","path":""}]}"#.to_vec() |
375 | 376 | }); |
376 | 377 |
|
377 | | - let mut response = (StatusCode::UNPROCESSABLE_ENTITY, body).into_response(); |
378 | | - response.headers_mut().insert( |
379 | | - CONTENT_TYPE, |
380 | | - ::axum::http::HeaderValue::from_static("application/json"), |
381 | | - ); |
382 | | - response |
| 378 | + ( |
| 379 | + StatusCode::UNPROCESSABLE_ENTITY, |
| 380 | + [(CONTENT_TYPE, HeaderValue::from_static("application/json"))], |
| 381 | + body, |
| 382 | + ) |
| 383 | + .into_response() |
383 | 384 | } |
0 commit comments