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 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(self) -> Result<(), ValidationError>
pub fn validate(self) -> Result<(), ValidationError>
Run validation and return errors if any.
Auto Trait Implementations§
impl<'a> Freeze for Validator<'a>
impl<'a> !RefUnwindSafe 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>
impl<'a> !UnwindSafe 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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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