-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return meaningful error message on Validation failure #20
Comments
Well, I actually ran into the same issue but thought it was related to the way formatting is handled by serde_json. In case it can be useful, here's how I solved it : I realize it doesn't belong to serde_json and could be added to this crate documentation, what do you think @c650 ? |
Wow @Roms1383 that's great. can I do this without implementing |
Haven't found a better way so far 😅 @c650 |
@Roms1383 that is why i made my own fork of this for now |
Any update on this? |
@quantumsheep @c650 I added some additional validation error information in 2.1 use actix_web::{web, App, HttpResponse, HttpServer, Responder, error};
use actix_web_validator::{QsQuery, QsQueryConfig, Error};
use serde::{Deserialize, Serialize};
use validator::Validate;
#[derive(Debug, Validate, Deserialize)]
pub struct Filter {
#[validate(length(min = 3))]
projects: Vec<i32>,
#[validate(length(min = 2))]
name: String,
}
async fn index(web::Path(path): web::Path<String>, query: QsQuery<Filter>) -> impl Responder {
dbg!(query);
HttpResponse::Ok().body(path)
}
#[derive(Serialize)]
pub struct ValidationErrorJsonPayload {
pub message: String,
pub fields: Vec<String>,
}
impl From<&validator::ValidationErrors> for ValidationErrorJsonPayload {
fn from(error: &validator::ValidationErrors) -> Self {
ValidationErrorJsonPayload {
message: "Validation error".to_owned(),
fields: error.field_errors().iter().map(|(field, _)| field.to_string()).collect(),
}
}
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
.app_data(QsQueryConfig::default().error_handler(|err, _| {
let json_error = match &err {
Error::Validate(error) => ValidationErrorJsonPayload::from(error),
_ => ValidationErrorJsonPayload { message: err.to_string(), fields: Vec::new() },
};
error::InternalError::from_response(err, HttpResponse::Conflict().json(json_error)).into()
}))
.service(web::resource("{path:.*}").route(web::get().to(index)))
})
.bind("127.0.0.1:8080")?
.run()
.await
} |
Sorry to disturb this thread but I'm a lit bit stuck trying to implement this myself. I've copied that example and when i cause a validation error, It never triggers the closure. I'm not even sure why I would. |
Hi, @NexRX! If it's actual can you show MRE? |
Ignore me, I'm no longer using actix so can't verify if it was just me doing something wrong. |
The code given in the comment is not correct and throws an error |
Current behaviour is that we just return BAD_REQUEST... However, this is not enough because then we don't know which field(s) failed validation.
Current code:
https://github.com/rambler-digital-solutions/actix-web-validator/blob/master/src/error.rs#L51-L55
If we change this method we can return a list of errors, or at least the first one
The text was updated successfully, but these errors were encountered: