[][src]Struct form_validation::AsyncValidatorFn

pub struct AsyncValidatorFn<Value, Key> { /* fields omitted */ }
This is supported on feature="async" only.

An function to perform validation on a field asynchonously.

For the synchronous version, see ValidatorFn.

Example

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

let v: AsyncValidatorFn<i32, String> =
    AsyncValidatorFn::new(|value: &i32, key: &String| {
        let key = key.clone();
        let value = *value;
        Box::pin(async move {
            // perform actions here that require async
            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(())
            }
        })
    });

let key = "field1".to_string();
assert!(block_on(v.validate_value(&20, &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!(
    "The value of field1 (-1) cannot be less than 0",
    error.to_string()
);
assert_eq!("NOT_LESS_THAN_0", error.type_id);

Implementations

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

pub fn new<C>(closure: C) -> Self where
    C: Fn(&Value, &Key) -> Pin<Box<dyn Future<Output = Result<(), ValidationErrors<Key>>>>> + 'static, 
[src]

Takes a closure that produces a Future that produces a ValidatorFn closure.

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

Runs the future to produce the ValidatorFn closure, and then performs the validation with that.

Trait Implementations

impl<Value, Key> Clone for AsyncValidatorFn<Value, Key>[src]

impl<Value, Key> Debug for AsyncValidatorFn<Value, Key>[src]

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

impl<Value, Key> PartialEq<AsyncValidatorFn<Value, Key>> for AsyncValidatorFn<Value, Key>[src]

Auto Trait Implementations

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

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

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

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

impl<Value, Key> !UnwindSafe for AsyncValidatorFn<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>,