use super::ValidationRule;
pub struct ValidationBuilder {
field: String,
rules: Vec<ValidationRule>,
}
impl ValidationBuilder {
pub fn new(field: impl Into<String>) -> Self {
Self {
field: field.into(),
rules: vec![],
}
}
pub fn required(mut self) -> Self {
self.rules.push(ValidationRule::Required);
self
}
pub fn email(mut self) -> Self {
self.rules.push(ValidationRule::Email);
self
}
pub fn url(mut self) -> Self {
self.rules.push(ValidationRule::Url);
self
}
pub fn min_length(mut self, len: usize) -> Self {
self.rules.push(ValidationRule::MinLength(len));
self
}
pub fn max_length(mut self, len: usize) -> Self {
self.rules.push(ValidationRule::MaxLength(len));
self
}
pub fn length(mut self, len: usize) -> Self {
self.rules.push(ValidationRule::Length(len));
self
}
pub fn min(mut self, val: f64) -> Self {
self.rules.push(ValidationRule::Min(val));
self
}
pub fn max(mut self, val: f64) -> Self {
self.rules.push(ValidationRule::Max(val));
self
}
pub fn range(mut self, min: f64, max: f64) -> Self {
self.rules.push(ValidationRule::Range(min, max));
self
}
pub fn regex(mut self, pattern: impl Into<String>) -> Self {
self.rules.push(ValidationRule::Regex(pattern.into()));
self
}
pub fn alpha(mut self) -> Self {
self.rules.push(ValidationRule::Alpha);
self
}
pub fn alphanumeric(mut self) -> Self {
self.rules.push(ValidationRule::Alphanumeric);
self
}
pub fn numeric(mut self) -> Self {
self.rules.push(ValidationRule::Numeric);
self
}
pub fn uuid(mut self) -> Self {
self.rules.push(ValidationRule::Uuid);
self
}
pub fn in_list(mut self, values: Vec<impl Into<String>>) -> Self {
self.rules.push(ValidationRule::In(
values.into_iter().map(|v| v.into()).collect(),
));
self
}
pub fn not_in(mut self, values: Vec<impl Into<String>>) -> Self {
self.rules.push(ValidationRule::NotIn(
values.into_iter().map(|v| v.into()).collect(),
));
self
}
pub fn custom(mut self, message: impl Into<String>) -> Self {
self.rules.push(ValidationRule::Custom(message.into()));
self
}
pub fn build(self) -> (String, Vec<ValidationRule>) {
(self.field, self.rules)
}
}