[][src]Struct form_validation::Validator

pub struct Validator<Value, Key> {
    pub validations: Vec<ValidatorFn<Value, Key>>,
}

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]

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]

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]

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]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,