pub struct AsyncValidator<Value, Key> {
pub validations: Vec<AsyncValidatorFn<Value, Key>>,
}
Available on crate feature
async
only.Expand description
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>>
Implementations§
Source§impl<Value, Key> AsyncValidator<Value, Key>
impl<Value, Key> AsyncValidator<Value, Key>
Sourcepub fn validation<F: Into<AsyncValidatorFn<Value, Key>> + 'static>(
self,
async_validator_fn: F,
) -> Self
pub fn validation<F: Into<AsyncValidatorFn<Value, Key>> + 'static>( 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>>
Trait Implementations§
Source§impl<Value: Clone, Key: Clone> Clone for AsyncValidator<Value, Key>
impl<Value: Clone, Key: Clone> Clone for AsyncValidator<Value, Key>
Source§fn clone(&self) -> AsyncValidator<Value, Key>
fn clone(&self) -> AsyncValidator<Value, Key>
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl<Value, Key> Default for AsyncValidator<Value, Key>
impl<Value, Key> Default for AsyncValidator<Value, Key>
Source§impl<Value, Key> From<Validator<Value, Key>> for AsyncValidator<Value, Key>
impl<Value, Key> From<Validator<Value, Key>> for AsyncValidator<Value, Key>
impl<Value, Key> StructuralPartialEq for AsyncValidator<Value, Key>
Auto Trait Implementations§
impl<Value, Key> Freeze for AsyncValidator<Value, Key>
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>
impl<Value, Key> !UnwindSafe for AsyncValidator<Value, Key>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more