Validate not triggered
Roms1383 opened this issue · 1 comments
Roms1383 commented
Hello,
I stumbled upon an issue that I can't really understand :
from my understanding, any type that implements validator::Validate
should be automatically validated (as long as it is Some
if inside an Option
) if wrapped in a actix_web_validator::Query
.
Context
logic.rs
// some more imports ...
use validator::{Validate, ValidationError, ValidationErrors}; // here I also tried to switch to actix_web_validator::Validate counterpart, unsuccessfully
#[derive(Deserialize)]
pub struct SomeOrderBy(pub String);
#[derive(Deserialize, Validate)]
pub struct Pagination<T: Validate> {
pub order_by: Option<T>,
}
impl Validate for SomeOrderBy {
fn validate(&self) -> Result<(), ValidationErrors> {
println!("validating for SomeOrderBy");
// validation logic ...
}
}
route.rs
// some more imports ...
use actix_web_validator::Query;
#[get("my-route")]
pub async fn my_route(
query: Query<Pagination<SomeOrderBy>>,
) -> impl Responder {
// route logic ...
}
Expected
Given a request with an unknown value for order_by
query parameter, I expect a failed validation which returns an early 400 - Bad Request.
GET /my-route?order_by=unknown
Got
The request is processed as if valid, and the validate
method of SomeOrderBy
is never triggered.
I might have messed up somewhere, if that's the case please provide some additional hints.
Thanks :)
Roms1383 commented
Ok my mistake I actually understood from the source code :
use validator;
use actix_web_validator;
// some more imports ...
#[derive(Deserialize)]
pub struct SomeOrderBy(pub String);
impl validator::Validate for SomeOrderBy {
fn validate(&self) -> Result<(), validator::ValidationErrors> {
println!("validating for SomeOrderBy");
// validation logic ...
}
}
#[derive(Deserialize, actix_web_validator::Validate)]
pub struct Pagination<T: validator::Validate> {
#[validate]
pub order_by: Option<T>,
}
It works 🎉
Feel free to close this issue.