优化校验流程

This commit is contained in:
李运家 2024-10-30 20:25:58 +08:00
parent 200e9e0e88
commit baccd91c50

View File

@ -1,7 +1,7 @@
use std::str::FromStr;
use i18n::{message, message_ids::MessageId};
use validator::Validate;
use validator::{Validate, ValidationErrors};
use crate::model::response::{ResData, ResErr, ResResult};
@ -15,21 +15,11 @@ pub fn validate_params(params: &impl Validate, local: &str) -> ResResult<ResData
for (_, err) in err.errors() {
match err {
validator::ValidationErrorsKind::Struct(err) => {
for (_field, errs) in err.field_errors() {
for e in errs {
let msg = e.message.clone().unwrap_or_default();
errors.push(message!(local, MessageId::from_str(msg.trim()).unwrap()));
}
}
pack_error_msg(&mut errors, err, local);
},
validator::ValidationErrorsKind::List(err) => {
for (_, err) in err.iter() {
for (_field, errs) in err.field_errors() {
for e in errs {
let msg = e.message.clone().unwrap_or_default();
errors.push(message!(local, MessageId::from_str(msg.trim()).unwrap()));
}
}
pack_error_msg(&mut errors, err, local);
}
},
validator::ValidationErrorsKind::Field(err) => {
@ -43,4 +33,13 @@ pub fn validate_params(params: &impl Validate, local: &str) -> ResResult<ResData
Err(ResErr::params(errors.join(";")))
},
}
}
fn pack_error_msg(errors: & mut Vec<&str>, raw_errors: &Box<ValidationErrors>, local: &str) {
for (_field, errs) in raw_errors.field_errors() {
for e in errs {
let msg = e.message.clone().unwrap_or_default();
errors.push(message!(local, MessageId::from_str(msg.trim()).unwrap()));
}
}
}