[][src]Struct valid::ValidationError

pub struct ValidationError {
    pub message: Option<Cow<'static, str>>,
    pub violations: Vec<ConstraintViolation>,
}

The error type returned if the validation finds any constraint violation.

It holds a list of constraint violations and an optional message. The message is meant to describe the context in which the validation has been performed. It is helpful when validating a struct that represents an input form or a REST command. In such cases the message would be something like "validating registration form" or "invalid post entry command".

The Display and Error traits are implemented to be compatible with most error management concepts. It can be converted into failure::Error using From or Into conversion traits.

It can be serialized and deserialized using the serde crate. To enable serde support the optional crate feature serde1 must be enabled.

Fields

message: Option<Cow<'static, str>>

Message that describes the context in which the validation has been executed

violations: Vec<ConstraintViolation>

A list of constraint violations found during validation

Methods

impl ValidationError[src]

pub fn merge(self, other: ValidationError) -> Self[src]

Merges this validation error with another validation error and returns a new validation error that contains all constraint violations from both errors merged into one list.

If both of the validation errors contain a message than the messages are concatenated separated by the string ' / '. If only one of the two errors contain a message than this message becomes the message of the resulting error.

Examples

use valid::{ValidationError, invalid_value};

let validation_error1 = ValidationError {
    message: Some("validating a user's age".into()),
    violations: vec![invalid_value("invalid-bound-min", "age", 12, 13)],
};
let validation_error2 = ValidationError {
    message: Some("validating a user registration command".into()),
    violations: vec![invalid_value("invalid-length-min", "username", 3, 4)],
};

let merged_error = validation_error2.merge(validation_error1);

assert_eq!(
    merged_error,
    ValidationError {
        message: Some(
            "validating a user registration command / validating a user's age".into()
        ),
        violations: vec![
            invalid_value("invalid-length-min", "username", 3, 4),
            invalid_value("invalid-bound-min", "age", 12, 13),
        ]
    }
);

Trait Implementations

impl Clone for ValidationError[src]

impl PartialEq<ValidationError> for ValidationError[src]

impl Display for ValidationError[src]

impl Debug for ValidationError[src]

impl Error for ValidationError[src]

impl Serialize for ValidationError[src]

impl<'de> Deserialize<'de> for ValidationError[src]

Auto Trait Implementations

Blanket Implementations

impl<T> ToString for T where
    T: Display + ?Sized
[src]

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

type Owned = T

The resulting type after obtaining ownership.

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

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

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> BorrowMut<T> for T where
    T: ?Sized
[src]

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]