use serde::Serialize;
use std::fmt::{self, Display};
#[derive(Debug, Serialize)]
pub struct ValidationError {
pub code: String,
pub message: String, }
impl fmt::Display for ValidationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}] {}", self.code, self.message)
}
}
impl From<ValidationError> for String {
fn from(err: ValidationError) -> Self {
format!("[{}] {}", err.code, err.message)
}
}
pub fn validate_field<T>(
field_name: &str,
value: Option<T>,
min: Option<T>,
max: Option<T>,
error_code_prefix: &str,
) -> Result<(), ValidationError>
where
T: PartialOrd + Display + Copy,
{
let val = value.ok_or(ValidationError {
code: format!("{}.{}.missing", error_code_prefix, field_name),
message: format!("{} must be provided.", field_name),
})?;
if let Some(min_val) = min {
if val < min_val {
return Err(ValidationError {
code: format!("{}.{}.too_small.{}", error_code_prefix, field_name, min_val),
message: format!(
"{} must be greater than or equal to {}.",
field_name, min_val
),
});
}
}
if let Some(max_val) = max {
if val > max_val {
return Err(ValidationError {
code: format!("{}.{}.too_large.{}", error_code_prefix, field_name, max_val),
message: format!("{} must be less than or equal to {}.", field_name, max_val),
});
}
}
Ok(())
}