[−][src]Struct form_validation::AsyncValidatorFn
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]
Key: Clone + PartialEq,
Value: Clone + PartialEq,
pub fn new<C>(closure: C) -> Self where
C: Fn(&Value, &Key) -> Pin<Box<dyn Future<Output = Result<(), ValidationErrors<Key>>>>> + 'static,
[src]
C: Fn(&Value, &Key) -> Pin<Box<dyn Future<Output = Result<(), ValidationErrors<Key>>>>> + 'static,
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]
&'_ self,
value: &'_ Value,
key: &'_ Key
) -> Result<(), ValidationErrors<Key>>
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]
fn clone(&self) -> Self
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[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]
Key: Clone + PartialEq + 'static,
Value: Clone + PartialEq + 'static,
fn from(validator_fn: ValidatorFn<Value, Key>) -> Self
[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,
Key: Unpin,
Value: Unpin,
impl<Value, Key> !UnwindSafe for AsyncValidatorFn<Value, Key>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,