[−][src]Struct form_validation::Validator
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
impl<Value, Key> Validator<Value, Key>
[src]
pub fn new() -> Self
[src]
Create a new Validator
.
pub fn validation<F: Into<ValidatorFn<Value, Key>> + 'static>(
self,
validator_fn: F
) -> Self
[src]
self,
validator_fn: F
) -> Self
A factory method to add a validation function to this validator.
Trait Implementations
impl<Value: Clone, Key: Clone> Clone for Validator<Value, Key>
[src]
impl<Value: Debug, Key: Debug> Debug for Validator<Value, Key>
[src]
impl<Value, Key> Default for Validator<Value, Key>
[src]
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, Key> PartialEq<Validator<Value, Key>> for Validator<Value, Key>
[src]
impl<Value, Key> Validation<Value, Key> for Validator<Value, Key> where
Key: PartialEq + Clone,
[src]
Key: PartialEq + Clone,
fn validate_value(
&self,
value: &Value,
key: &Key
) -> Result<(), ValidationErrors<Key>>
[src]
&self,
value: &Value,
key: &Key
) -> Result<(), ValidationErrors<Key>>
Auto Trait Implementations
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
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>,