pub struct Validator<Value, Key> {
pub validations: Vec<ValidatorFn<Value, Key>>,
}
Expand description
Validates a particular type of value, can contain many validation functions. Generally used with a single key for all contained validation functions.
§Example
use form_validation::{Validation, ValidationError, Validator};
let v: Validator<i32, String> = Validator::new()
.validation(|value: &i32, 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(())
}
})
.validation(|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())
} else {
Ok(())
}
});
let key = "field1".to_string();
{
let errors = 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!(v.validate_value(&5, &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!("NOT_LESS_THAN_0", error.type_id);
}
Fields§
§validations: Vec<ValidatorFn<Value, Key>>
Implementations§
Source§impl<Value, Key> Validator<Value, Key>
impl<Value, Key> Validator<Value, Key>
Sourcepub fn validation<F: Into<ValidatorFn<Value, Key>> + 'static>(
self,
validator_fn: F,
) -> Self
pub fn validation<F: Into<ValidatorFn<Value, Key>> + 'static>( self, validator_fn: F, ) -> Self
A factory method to add a validation function to this validator.
Trait Implementations§
Source§impl<Value, Key> From<Validator<Value, Key>> for AsyncValidator<Value, Key>
impl<Value, Key> From<Validator<Value, Key>> for AsyncValidator<Value, Key>
Source§impl<Value, Key> Validation<Value, Key> for Validator<Value, Key>
impl<Value, Key> Validation<Value, Key> for Validator<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 Validator<Value, Key>
impl<Value, Key> !RefUnwindSafe for Validator<Value, Key>
impl<Value, Key> !Send for Validator<Value, Key>
impl<Value, Key> !Sync for Validator<Value, Key>
impl<Value, Key> Unpin for Validator<Value, Key>
impl<Value, Key> !UnwindSafe for Validator<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