karbon_framework/validation/
validator.rs1use crate::error::AppError;
2use validator::Validate;
3
4pub fn validate_request<T: Validate>(input: &T) -> Result<(), AppError> {
6 input.validate().map_err(|errors| {
7 let messages: Vec<String> = errors
8 .field_errors()
9 .iter()
10 .flat_map(|(field, errs)| {
11 errs.iter().map(move |e| {
12 let msg = e
13 .message
14 .as_ref()
15 .map(|m| m.to_string())
16 .unwrap_or_else(|| format!("Invalid value for {}", field));
17 format!("{}: {}", field, msg)
18 })
19 })
20 .collect();
21
22 AppError::Validation(messages.join(", "))
23 })
24}
25
26#[inline]
28pub fn validate_input<T: Validate>(input: &T) -> Result<(), AppError> {
29 validate_request(input)
30}