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
impl Validation
Sourcepub fn new() -> Validation
pub fn new() -> Validation
Creates a validation.
Sourcepub fn max_errors(self, max: usize) -> Validation
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.
Sourcepub fn setup(&self, driver: &mut DeserializeDriver<'_, '_>)
pub fn setup(&self, driver: &mut DeserializeDriver<'_, '_>)
Sets up a deserialization.
Trait Implementations§
Source§impl Debug for Validation
impl Debug for Validation
Auto Trait Implementations§
impl Freeze for Validation
impl RefUnwindSafe for Validation
impl Send for Validation
impl Sync for Validation
impl Unpin for Validation
impl UnsafeUnpin for Validation
impl UnwindSafe for Validation
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