pub struct ValidatorFn<Value, Key> { /* private fields */ }
Expand description
Function to perform validation on a form field.
§Example
use form_validation::{Validation, ValidationError, ValidatorFn};
let v: ValidatorFn<i32, String> = ValidatorFn::new(|value, key: &String| {
if value < &0 {
let value_clone = *value;
Err(ValidationError::new(key.clone(), "NOT_LESS_THAN_0")
.with_message(move |key| {
format!(
"The value of {} ({}) cannot be less than 0",
key, value_clone
)
}).into()) // convert into ValidationErrors
} else {
Ok(())
}
});
let key = "field1".to_string();
assert!(v.validate_value(&20, &key).is_ok());
let errors = 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§
Trait Implementations§
Source§impl<Value, Key> Clone for ValidatorFn<Value, Key>
impl<Value, Key> Clone for ValidatorFn<Value, Key>
Source§impl<Value, Key> Debug for ValidatorFn<Value, Key>
impl<Value, Key> Debug for ValidatorFn<Value, Key>
Source§impl<C, Value, Key> From<C> for ValidatorFn<Value, Key>
impl<C, Value, Key> From<C> for ValidatorFn<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 ValidatorFn<Value, Key>
impl<Value, Key> PartialEq for ValidatorFn<Value, Key>
Source§impl<Value, Key> Validation<Value, Key> for ValidatorFn<Value, Key>
impl<Value, Key> Validation<Value, Key> for ValidatorFn<Value, Key>
Source§fn validate_value(
&self,
value: &Value,
key: &Key,
) -> Result<(), ValidationErrors<Key>>
fn validate_value( &self, value: &Value, key: &Key, ) -> Result<(), ValidationErrors<Key>>
Validate a given form field referenced by a given
Key
, that
contains a given Value
, returns
ValidationErrors if there are any.Auto Trait Implementations§
impl<Value, Key> Freeze for ValidatorFn<Value, Key>
impl<Value, Key> !RefUnwindSafe for ValidatorFn<Value, Key>
impl<Value, Key> !Send for ValidatorFn<Value, Key>
impl<Value, Key> !Sync for ValidatorFn<Value, Key>
impl<Value, Key> Unpin for ValidatorFn<Value, Key>
impl<Value, Key> !UnwindSafe for ValidatorFn<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