pub trait Validate {
// Required method
fn validate_with_group(
&self,
group: ValidationGroup,
) -> Result<(), ValidationErrors>;
// Provided methods
fn validate(&self) -> Result<(), ValidationErrors> { ... }
fn validated(self) -> Result<Self, ValidationErrors>
where Self: Sized { ... }
fn validated_with_group(
self,
group: ValidationGroup,
) -> Result<Self, ValidationErrors>
where Self: Sized { ... }
}Expand description
Trait for synchronous validation of a struct.
Implement this trait to enable validation on your types.
§Example
ⓘ
use rustapi_validate::v2::prelude::*;
struct User {
email: String,
age: u8,
}
impl Validate for User {
fn validate(&self) -> Result<(), ValidationErrors> {
let mut errors = ValidationErrors::new();
if let Err(e) = EmailRule::default().validate(&self.email) {
errors.add("email", e);
}
if let Err(e) = RangeRule::new(18, 120).validate(&self.age) {
errors.add("age", e);
}
errors.into_result()
}
}Required Methods§
Sourcefn validate_with_group(
&self,
group: ValidationGroup,
) -> Result<(), ValidationErrors>
fn validate_with_group( &self, group: ValidationGroup, ) -> Result<(), ValidationErrors>
Validate the struct with a specific validation group.
Provided Methods§
Sourcefn validate(&self) -> Result<(), ValidationErrors>
fn validate(&self) -> Result<(), ValidationErrors>
Validate the struct synchronously with the default group.
Sourcefn validated(self) -> Result<Self, ValidationErrors>where
Self: Sized,
fn validated(self) -> Result<Self, ValidationErrors>where
Self: Sized,
Validate and return the struct if valid.
Sourcefn validated_with_group(
self,
group: ValidationGroup,
) -> Result<Self, ValidationErrors>where
Self: Sized,
fn validated_with_group(
self,
group: ValidationGroup,
) -> Result<Self, ValidationErrors>where
Self: Sized,
Validate and return the struct if valid (with group).