ValidatorBuilder

Struct ValidatorBuilder 

Source
pub struct ValidatorBuilder<T> { /* private fields */ }
Expand description

Helper struct to build validators in a fluent style

Implementations§

Source§

impl<T> ValidatorBuilder<T>

Source

pub fn new() -> Self

Create a new validator builder

Source

pub fn rule_for<F, V>( self, _property_name: impl Into<String>, accessor: F, builder: RuleBuilder<V>, ) -> Self
where F: Fn(&T) -> &V + 'static, V: 'static,

Add a rule for a property

Source

pub fn must<F, V, P>( self, property_name: impl Into<String>, accessor: F, predicate: P, message: impl Into<String>, ) -> Self
where F: Fn(&T) -> &V + 'static, V: 'static, P: Fn(&T, &V) -> bool + 'static,

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 validated
  • accessor - Function to access the property value from the object
  • predicate - Function that receives both the entire object and the property value, returns true if valid
  • message - 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")
Source

pub fn build(self) -> impl Validator<T>

Build the validator

Trait Implementations§

Source§

impl<T> Default for ValidatorBuilder<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.