-
SummaryI use validator to validate my every request struct, such as pub async fn create(Json(payload): Json<CreateUserRequest>) -> Json<CreateUserResponse> {
//payload.validate() and some logic on validate result
}
pub async fn update(Json(payload): Json<UpdateUserRequest>) -> Json<UpdateUserResponse> {
//payload.validate() and some logic on validate result
} I don't want to do so everytime after adding new handler, so I wonder whether there is a way to do this all in one extractor with Generics, such as : pub struct Validated(pub Json)? and the handler looks like pub async fn update(Validated(Json(payload)): Validated<Json<UpdateUserRequest>>) -> Json<UpdateUserResponse> {} Thanks everyone, appreciate you all. axum version0.7.5 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
Did you try impl<T> FromRequest for Validated<T> {
// ...
} and implementing the required method? Was there some issue with it? I'm not sure though if an impl like that would actually help you since you can't have different validations for different payloads that way and thus you can check just common things (e.g. common fields are present). So you might actually want to do something like impl FromRequest for Validated<CreateUserRequest> {
// ...
}
impl FromRequest for Validated<UpdateUserRequest> {
// ...
} |
Beta Was this translation helpful? Give feedback.
maybe something like this