Struct Validation

Source
pub struct Validation<C, T>(/* private fields */);
Expand description

State of an ongoing validation.

It provides combinator methods like and and and_then to combine validation steps to complex validations and accumulates all constraint violations found by the executed validations.

The result of a validation can be obtained by calling the result method.

see the crate level documentation for details and examples on how to use the methods provided by this struct.

Implementations§

Source§

impl<C, T> Validation<C, T>

Source

pub fn success(valid: T) -> Self

Constructs a Validation for a successful validation step.

This method is provided to enable users of this crate to implement custom validation functions.

Source

pub fn failure( constraint_violations: impl IntoIterator<Item = ConstraintViolation>, ) -> Self

Constructs a Validation for a failed validation step.

This method is provided to enable users of this crate to implement custom validation functions.

Source

pub fn result(self) -> ValidationResult<C, T>

Finishes a validation and returns the result of the validation.

A validation may comprise multiple validation steps that are combined using the combinator methods of this struct. After all steps are executed this method can be called to get the ValidationResult

Source

pub fn with_message( self, message: impl Into<Cow<'static, str>>, ) -> ValidationResult<C, T>

Finishes a validation providing a message and returns the result.

A validation may comprise multiple validation steps that are combined using the combinator methods of this struct. After all steps are executed this method can be called to get the ValidationResult

In case of an error the ValidationError will contain the given message. It is meant to describe the context in which the validation has been executed. E.g when validating a struct that represents an input form the message would be something like “validating registration form” or when validating a struct that represents a REST command the message would be something like “invalid post entry command”.

Source

pub fn combine<U>(self, value: U) -> Validation<C, (U, T)>

Combines a value that needs no further validation with the validation result.

This method may be especially useful in combination with the and_then combinator method. See the crate level documentation for an example.

Source

pub fn map<D, U>(self, convert: impl Fn(T) -> U) -> Validation<D, U>

Maps the validated values into another type.

This method is used for complex validations that validate multiple fields of a struct and the result should be mapped back into this struct. See the crate level documentation for an example.

Source

pub fn and<D, U>(self, other: Validation<D, U>) -> Validation<D, (T, U)>

Combines this validation with another validation unconditionally.

The other validation is executed regardless whether this validation has been successful or not.

The resulting validation is only successful if itself was successful and the other validation is also successful. Any constraint violations found either by this validation or the other validation are accumulated.

See the crate level documentation for an example.

Source

pub fn and_then<D, U>( self, next: impl FnOnce(T) -> Validation<D, U>, ) -> Validation<D, U>

Combines this validation with another validation conditionally.

The other validation is only executed if this validation has been successful. It has access to the values that have been validated so far.

Those values are provided in a tuple as an argument to the given closure. If there is one value that has been validated so far the argument T to the closure is simple the type of the value. In case of two values T is a tuple of type (A, B), in case of 3 values the type of T is a tuple of a tuple and the 3rd value like ((A, B), C) and so on.

Values that are given as argument to the closure but not used for the other validation are not part of the final result of the validation. To add unused values to the result of the validation we can use the combine method.

See the crate level documentation for an example.

Trait Implementations§

Source§

impl<C, T> Debug for Validation<C, T>
where C: Debug, T: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<C: PartialEq, T: PartialEq> PartialEq for Validation<C, T>

Source§

fn eq(&self, other: &Validation<C, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<C, T> StructuralPartialEq for Validation<C, T>

Auto Trait Implementations§

§

impl<C, T> Freeze for Validation<C, T>
where T: Freeze,

§

impl<C, T> RefUnwindSafe for Validation<C, T>

§

impl<C, T> Send for Validation<C, T>
where T: Send, C: Send,

§

impl<C, T> Sync for Validation<C, T>
where T: Sync, C: Sync,

§

impl<C, T> Unpin for Validation<C, T>
where T: Unpin, C: Unpin,

§

impl<C, T> UnwindSafe for Validation<C, T>
where T: UnwindSafe, C: UnwindSafe,

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.