pub struct ValidatorBuilder<T> { /* private fields */ }Expand description
Helper struct to build validators in a fluent style
Implementations§
Source§impl<T> ValidatorBuilder<T>
impl<T> ValidatorBuilder<T>
Sourcepub fn rule_for<F, V>(
self,
_property_name: impl Into<String>,
accessor: F,
builder: RuleBuilder<V>,
) -> Self
pub fn rule_for<F, V>( self, _property_name: impl Into<String>, accessor: F, builder: RuleBuilder<V>, ) -> Self
Add a rule for a property
Sourcepub fn must<F, V, P>(
self,
property_name: impl Into<String>,
accessor: F,
predicate: P,
message: impl Into<String>,
) -> Self
pub fn must<F, V, P>( self, property_name: impl Into<String>, accessor: F, predicate: P, message: impl Into<String>, ) -> Self
Add a rule for a property that can access the entire object
This allows you to validate a property based on other properties in the object. The closure receives both the object and the property value.
§Arguments
property_name- Name of the property being validatedaccessor- Function to access the property value from the objectpredicate- Function that receives both the entire object and the property value, returns true if validmessage- Error message to use if validation fails
§Example
ⓘ
// Validate property using both object and property value
.must("taxNumber", |c| &c.tax_number,
|command, tax_number| tax_number.is_valid_tax_number(&command.country_iso_code),
"Tax number is not valid for the specified country")
// Validate property ignoring the object (use _ for object parameter)
.must("country", |c| &c.country,
|_, country| Countries::allowed_countries().contains(country),
"Country is not in the allowed list")Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for ValidatorBuilder<T>
impl<T> !RefUnwindSafe for ValidatorBuilder<T>
impl<T> !Send for ValidatorBuilder<T>
impl<T> !Sync for ValidatorBuilder<T>
impl<T> Unpin for ValidatorBuilder<T>
impl<T> !UnwindSafe for ValidatorBuilder<T>
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