pub struct AsyncValidatorFn<Value, Key> { /* private fields */ }
Available on crate feature
async
only.Expand description
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§
Source§impl<Value, Key> AsyncValidatorFn<Value, Key>
impl<Value, Key> AsyncValidatorFn<Value, Key>
Sourcepub fn new<C>(closure: C) -> Self
pub fn new<C>(closure: C) -> Self
Takes a closure that produces a Future
that produces a
ValidatorFn closure.
Sourcepub async fn validate_value(
&self,
value: &Value,
key: &Key,
) -> Result<(), ValidationErrors<Key>>
pub async fn validate_value( &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§
Source§impl<Value, Key> Clone for AsyncValidatorFn<Value, Key>
impl<Value, Key> Clone for AsyncValidatorFn<Value, Key>
Source§impl<Value, Key> Debug for AsyncValidatorFn<Value, Key>
impl<Value, Key> Debug for AsyncValidatorFn<Value, Key>
Source§impl<Value, Key> From<ValidatorFn<Value, Key>> for AsyncValidatorFn<Value, Key>
impl<Value, Key> From<ValidatorFn<Value, Key>> for AsyncValidatorFn<Value, Key>
Source§fn from(validator_fn: ValidatorFn<Value, Key>) -> Self
fn from(validator_fn: ValidatorFn<Value, Key>) -> Self
Converts to this type from the input type.
Source§impl<Value, Key> PartialEq for AsyncValidatorFn<Value, Key>
impl<Value, Key> PartialEq for AsyncValidatorFn<Value, Key>
Auto Trait Implementations§
impl<Value, Key> Freeze for AsyncValidatorFn<Value, Key>
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>
impl<Value, Key> !UnwindSafe for AsyncValidatorFn<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