[−][src]Struct form_validation::AsyncValidator
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]
Key: Clone + PartialEq,
Value: Clone + PartialEq,
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]
self,
async_validator_fn: F
) -> Self
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]
&'_ self,
value: &'_ Value,
key: &'_ Key
) -> Result<(), ValidationErrors<Key>>
Trait Implementations
impl<Value: Clone, Key: Clone> Clone for AsyncValidator<Value, Key>
[src]
fn clone(&self) -> AsyncValidator<Value, Key>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[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]
Key: Clone + PartialEq,
Value: Clone + PartialEq,
impl<Value, Key> From<Validator<Value, Key>> for AsyncValidator<Value, Key> where
Value: Clone + PartialEq + 'static,
Key: Clone + PartialEq + 'static,
[src]
Value: Clone + PartialEq + 'static,
Key: Clone + PartialEq + 'static,
impl<Value: PartialEq, Key: PartialEq> PartialEq<AsyncValidator<Value, Key>> for AsyncValidator<Value, Key>
[src]
fn eq(&self, other: &AsyncValidator<Value, Key>) -> bool
[src]
fn ne(&self, other: &AsyncValidator<Value, Key>) -> bool
[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,
Key: Unpin,
Value: Unpin,
impl<Value, Key> !UnwindSafe for AsyncValidator<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>,