pub struct Validator<'a> { /* private fields */ }Expand description
Request validator.
§Example
use ferro_rs::validation::{Validator, rules::*};
let data = serde_json::json!({
"email": "user@example.com",
"password": "secret123",
"password_confirmation": "secret123"
});
let result = Validator::new(&data)
.rules("email", vec![required(), email()])
.rules("password", vec![required(), min(8), confirmed()])
.validate();
match result {
Ok(()) => println!("Validation passed!"),
Err(errors) => println!("Errors: {:?}", errors),
}Implementations§
Source§impl<'a> Validator<'a>
impl<'a> Validator<'a>
Sourcepub fn with_error(
self,
field: impl Into<String>,
message: impl Into<String>,
) -> Self
pub fn with_error( self, field: impl Into<String>, message: impl Into<String>, ) -> Self
Pre-seed a validation error for a field without adding a rule.
Useful for cross-field checks (e.g. password confirmation) performed
before calling validate(). The error appears in the final result
alongside any rule-based errors.
§Example
let mut validator = Validator::new(&data)
.rules("password", rules![required(), min(8)]);
if form.password != form.password_confirmation {
validator = validator.with_error("password_confirmation", "Passwords do not match.");
}
validator.validate()?;Sourcepub fn rule<R: Rule + 'static>(self, field: impl Into<String>, rule: R) -> Self
pub fn rule<R: Rule + 'static>(self, field: impl Into<String>, rule: R) -> Self
Add a single validation rule for a field.
Sourcepub fn boxed_rules(
self,
field: impl Into<String>,
rules: Vec<Box<dyn Rule>>,
) -> Self
pub fn boxed_rules( self, field: impl Into<String>, rules: Vec<Box<dyn Rule>>, ) -> Self
Add boxed rules for a field (useful for dynamic rule creation).
Sourcepub fn messages(self, messages: HashMap<String, String>) -> Self
pub fn messages(self, messages: HashMap<String, String>) -> Self
Set custom messages from a map.
Sourcepub fn attributes(self, attributes: HashMap<String, String>) -> Self
pub fn attributes(self, attributes: HashMap<String, String>) -> Self
Set custom attributes from a map.
Sourcepub fn stop_on_first_failure(self) -> Self
pub fn stop_on_first_failure(self) -> Self
Stop validating remaining fields after first failure.
Sourcepub fn validate_or_redirect(
self,
url: impl Into<String>,
) -> Result<(), ActionError>
pub fn validate_or_redirect( self, url: impl Into<String>, ) -> Result<(), ActionError>
Validate and, on failure, flash per-field errors + old input and return
an crate::http::action::ActionError redirecting to url.
Composes the existing with_old_input + into_action_error chain so
per-field errors and old input flash exactly as the modern form-error
idiom does today. The validator already holds the data passed to
Validator::new, so it is reused here without requiring the caller
to pass it again.
Validator::new(&data)
.rules("name", rules![required()])
.validate_or_redirect(&form_url)?;Sourcepub fn validate(self) -> Result<(), ValidationError>
pub fn validate(self) -> Result<(), ValidationError>
Run validation and return errors if any.
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for Validator<'a>
impl<'a> !UnwindSafe for Validator<'a>
impl<'a> Freeze for Validator<'a>
impl<'a> Send for Validator<'a>
impl<'a> Sync for Validator<'a>
impl<'a> Unpin for Validator<'a>
impl<'a> UnsafeUnpin for Validator<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more