[][src]Struct form_validation::AsyncValidator

pub struct AsyncValidator<Value, Key> {
    pub validations: Vec<AsyncValidatorFn<Value, Key>>,
}
This is supported on feature="async" only.

Validates a particular type of value asynchronously, can contain many validation functions. Generally used with a single key for all contained validation functions.

See Validator for the synchronous version.

use form_validation::{AsyncValidator, ValidationError, AsyncValidatorFn, ValidatorFn};
use futures::executor::block_on;

let v: AsyncValidator<i32, String> = AsyncValidator::new()
    .validation(AsyncValidatorFn::new(|value: &i32, key: &String| {
        let value = *value;
        let key = key.clone();
        Box::pin(async move {
            if value < 0 {
                Err(ValidationError::new(key.clone(), "NOT_LESS_THAN_0")
                    .with_message(move |key| {
                        format!("The value of {} ({}) cannot be less than 0", key, value)
                    })
                    .into()) // convert into ValidationErrors
            } else {
                Ok(())
            }
        })
    }))
    // also supports compatibility with the synchronous ValidatorFn
    .validation(ValidatorFn::new(|value: &i32, key: &String| {
        if value > &10 {
            let value_clone = *value;
            Err(ValidationError::new(key.clone(), "NOT_GREATER_THAN_10")
                .with_message(move |key| {
                    format!(
                        "The value of {} ({}) cannot be greater than 10",
                        key, value_clone
                    )
                })
                .into()) // convert into ValidationErrors
        } else {
            Ok(())
        }
    }));
let key = "field1".to_string();
{
    let errors = block_on(v.validate_value(&11, &key)).unwrap_err();
    assert_eq!(1, errors.len());
    let error = errors.errors.get(0).unwrap();
    assert_eq!("NOT_GREATER_THAN_10", error.type_id);
}

assert!(block_on(v.validate_value(&5, &key)).is_ok());

{
    let errors = block_on(v.validate_value(&-1, &key)).unwrap_err();
    assert_eq!(1, errors.len());
    let error = errors.errors.get(0).unwrap();
    assert_eq!("NOT_LESS_THAN_0", error.type_id);
}

Fields

validations: Vec<AsyncValidatorFn<Value, Key>>
This is supported on feature="async" only.

Implementations

impl<Value, Key> AsyncValidator<Value, Key> where
    Key: Clone + PartialEq,
    Value: Clone + PartialEq
[src]

pub fn new() -> Self[src]

Create a new Validator.

pub fn validation<F: Into<AsyncValidatorFn<Value, Key>> + 'static>(
    self,
    async_validator_fn: F
) -> Self
[src]

A factory method to add a validation function to this validator.

pub async fn validate_value<'_, '_, '_>(
    &'_ self,
    value: &'_ Value,
    key: &'_ Key
) -> Result<(), ValidationErrors<Key>>
[src]

Trait Implementations

impl<Value: Clone, Key: Clone> Clone for AsyncValidator<Value, Key>[src]

impl<Value: Debug, Key: Debug> Debug for AsyncValidator<Value, Key>[src]

impl<Value, Key> Default for AsyncValidator<Value, Key> where
    Key: Clone + PartialEq,
    Value: Clone + PartialEq
[src]

impl<Value, Key> From<Validator<Value, Key>> for AsyncValidator<Value, Key> where
    Value: Clone + PartialEq + 'static,
    Key: Clone + PartialEq + 'static, 
[src]

impl<Value: PartialEq, Key: PartialEq> PartialEq<AsyncValidator<Value, Key>> for AsyncValidator<Value, Key>[src]

impl<Value, Key> StructuralPartialEq for AsyncValidator<Value, Key>[src]

Auto Trait Implementations

impl<Value, Key> !RefUnwindSafe for AsyncValidator<Value, Key>

impl<Value, Key> !Send for AsyncValidator<Value, Key>

impl<Value, Key> !Sync for AsyncValidator<Value, Key>

impl<Value, Key> Unpin for AsyncValidator<Value, Key> where
    Key: Unpin,
    Value: Unpin

impl<Value, Key> !UnwindSafe for AsyncValidator<Value, Key>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,