pub struct Validator { /* private fields */ }Expand description
Fluent builder that collects field errors and converts to AppError via Validator::validate.
Implementations§
Source§impl Validator
impl Validator
Sourcepub fn required(self, field: &str, value: &str) -> Self
pub fn required(self, field: &str, value: &str) -> Self
Fail if value is empty or whitespace-only.
Sourcepub fn min_length(self, field: &str, value: &str, min: usize) -> Self
pub fn min_length(self, field: &str, value: &str, min: usize) -> Self
Fail if value has fewer than min characters.
Sourcepub fn max_length(self, field: &str, value: &str, max: usize) -> Self
pub fn max_length(self, field: &str, value: &str, max: usize) -> Self
Fail if value exceeds max characters.
Sourcepub fn email(self, field: &str, value: &str) -> Self
pub fn email(self, field: &str, value: &str) -> Self
Fail if value is not a valid e-mail address.
Sourcepub fn url(self, field: &str, value: &str) -> Self
pub fn url(self, field: &str, value: &str) -> Self
Fail if value is not a valid HTTP/HTTPS URL.
Sourcepub fn pattern(self, field: &str, value: &str, re: &str) -> Self
pub fn pattern(self, field: &str, value: &str, re: &str) -> Self
Fail if value does not match the regular expression re.
Sourcepub fn required_uuid(self, field: &str, value: &str) -> Self
pub fn required_uuid(self, field: &str, value: &str) -> Self
Fail if value is not a valid UUID string.
Sourcepub fn optional_uuid(self, field: &str, value: Option<&str>) -> Self
pub fn optional_uuid(self, field: &str, value: Option<&str>) -> Self
Fail if value is Some but not a valid UUID string.
Sourcepub fn in_range<T: PartialOrd + Display>(
self,
field: &str,
value: T,
min: T,
max: T,
) -> Self
pub fn in_range<T: PartialOrd + Display>( self, field: &str, value: T, min: T, max: T, ) -> Self
Fail if value is outside the inclusive range [min, max].
Sourcepub fn min_value<T: PartialOrd + Display>(
self,
field: &str,
value: T,
min: T,
) -> Self
pub fn min_value<T: PartialOrd + Display>( self, field: &str, value: T, min: T, ) -> Self
Fail if value is below min.
Sourcepub fn max_value<T: PartialOrd + Display>(
self,
field: &str,
value: T,
max: T,
) -> Self
pub fn max_value<T: PartialOrd + Display>( self, field: &str, value: T, max: T, ) -> Self
Fail if value is above max.
Sourcepub fn before(self, field: &str, value: &str, deadline: &str) -> Self
pub fn before(self, field: &str, value: &str, deadline: &str) -> Self
Fail if value (ISO-8601 datetime string) is not before deadline.
Sourcepub fn after(self, field: &str, value: &str, floor: &str) -> Self
pub fn after(self, field: &str, value: &str, floor: &str) -> Self
Fail if value (ISO-8601 datetime string) is not after floor.
Sourcepub fn one_of<T: PartialEq + Display>(
self,
field: &str,
value: &T,
allowed: &[T],
) -> Self
pub fn one_of<T: PartialEq + Display>( self, field: &str, value: &T, allowed: &[T], ) -> Self
Fail if value is not in allowed.
Sourcepub fn custom(self, condition: bool, field: &str, message: &str) -> Self
pub fn custom(self, condition: bool, field: &str, message: &str) -> Self
Add an error for field if condition is false.
Sourcepub fn has_errors(&self) -> bool
pub fn has_errors(&self) -> bool
Returns true if any validation errors have been accumulated.
Sourcepub fn errors(&self) -> &[FieldError]
pub fn errors(&self) -> &[FieldError]
Returns a slice of all accumulated field errors.
Sourcepub fn validate(self) -> AppResult<()>
pub fn validate(self) -> AppResult<()>
Consume the validator and return Ok(()) if no errors,
or an AppError::invalid_input containing all field errors.