use crate::ConstraintViolationList;
pub trait Constraint: Send + Sync {
fn code(&self) -> &'static str;
fn validate(&self, value: &str, property_path: &str, violations: &mut ConstraintViolationList);
}
#[derive(Debug, Default, Clone, Copy)]
pub struct NotBlank;
impl Constraint for NotBlank {
fn code(&self) -> &'static str {
"NotBlank"
}
fn validate(&self, value: &str, property_path: &str, violations: &mut ConstraintViolationList) {
if value.trim().is_empty() {
violations.add(crate::Violation::new(
property_path,
"This value should not be blank.",
self.code(),
));
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Length {
pub min: usize,
pub max: usize,
}
impl Length {
#[must_use]
pub const fn new(min: usize, max: usize) -> Self {
Self { min, max }
}
}
impl Constraint for Length {
fn code(&self) -> &'static str {
"Length"
}
fn validate(&self, value: &str, property_path: &str, violations: &mut ConstraintViolationList) {
let len = value.chars().count();
if len < self.min || len > self.max {
violations.add(crate::Violation::new(
property_path,
format!(
"This value should have between {} and {} characters.",
self.min, self.max
),
self.code(),
));
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Range {
pub min: i64,
pub max: i64,
}
impl Range {
#[must_use]
pub const fn new(min: i64, max: i64) -> Self {
Self { min, max }
}
}
impl Constraint for Range {
fn code(&self) -> &'static str {
"Range"
}
fn validate(&self, value: &str, property_path: &str, violations: &mut ConstraintViolationList) {
let Ok(parsed) = value.parse::<i64>() else {
violations.add(crate::Violation::new(
property_path,
"This value should be a valid integer.",
self.code(),
));
return;
};
if parsed < self.min || parsed > self.max {
violations.add(crate::Violation::new(
property_path,
format!(
"This value should be between {} and {}.",
self.min, self.max
),
self.code(),
));
}
}
}