优化校验流程
This commit is contained in:
parent
200e9e0e88
commit
baccd91c50
@ -1,7 +1,7 @@
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use i18n::{message, message_ids::MessageId};
|
use i18n::{message, message_ids::MessageId};
|
||||||
use validator::Validate;
|
use validator::{Validate, ValidationErrors};
|
||||||
|
|
||||||
use crate::model::response::{ResData, ResErr, ResResult};
|
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() {
|
for (_, err) in err.errors() {
|
||||||
match err {
|
match err {
|
||||||
validator::ValidationErrorsKind::Struct(err) => {
|
validator::ValidationErrorsKind::Struct(err) => {
|
||||||
for (_field, errs) in err.field_errors() {
|
pack_error_msg(&mut errors, err, local);
|
||||||
for e in errs {
|
|
||||||
let msg = e.message.clone().unwrap_or_default();
|
|
||||||
errors.push(message!(local, MessageId::from_str(msg.trim()).unwrap()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
validator::ValidationErrorsKind::List(err) => {
|
validator::ValidationErrorsKind::List(err) => {
|
||||||
for (_, err) in err.iter() {
|
for (_, err) in err.iter() {
|
||||||
for (_field, errs) in err.field_errors() {
|
pack_error_msg(&mut errors, err, local);
|
||||||
for e in errs {
|
|
||||||
let msg = e.message.clone().unwrap_or_default();
|
|
||||||
errors.push(message!(local, MessageId::from_str(msg.trim()).unwrap()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
validator::ValidationErrorsKind::Field(err) => {
|
validator::ValidationErrorsKind::Field(err) => {
|
||||||
@ -44,3 +34,12 @@ pub fn validate_params(params: &impl Validate, local: &str) -> ResResult<ResData
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user