Skip to main content

Validation

Struct Validation 

Source
pub struct Validation { /* private fields */ }
Expand description

Finds all problems of an input.

A validation is set up on a deserialization (with the setup function that formats accept, see Deserializer::deserialize_with). It collects errors (see State::set_collect_errors) and tracks paths (see PathLayer). Once the deserialization is done, finish returns the value together with a Report of all problems: the errors that failed the deserialization and the errors that Validated values kept.

use deser::Deserialize;
use deser_validate::{Email, Range, Validated, Validation};

#[derive(Deserialize)]
struct Signup {
    email: Validated<String, Email>,
    age: Validated<u8, Range<13, 130>>,
    name: String,
}

let input = r#"{"email": "nope", "age": 7}"#;
let validation = Validation::new();
let rv = deser_json::Deserializer::from_str(input)
    .deserialize_with::<Signup, _>(|driver| validation.setup(driver));
let outcome = validation.finish(rv);
assert!(outcome.value.is_none());
assert_eq!(
    outcome.report.to_string(),
    "email: invalid value: must be an email address (at offset 10)\n\
     age: invalid value: must be between 13 and 130 (at offset 25)\n\
     missing field `name` (at line 1 column 27)"
);

Implementations§

Source§

impl Validation

Source

pub fn new() -> Validation

Creates a validation.

Source

pub fn max_errors(self, max: usize) -> Validation

Limits the number of errors that are collected.

See State::set_max_errors. This limits the errors that fail the deserialization, not the errors that values keep.

Source

pub fn setup(&self, driver: &mut DeserializeDriver<'_, '_>)

Sets up a deserialization.

Source

pub fn finish<T>(&self, rv: Result<T, Error>) -> Outcome<T>

Finishes the validation with the result of the deserialization.

Trait Implementations§

Source§

impl Debug for Validation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Validation

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.