pub struct Validated<T, V = ()> { /* private fields */ }Expand description
A value that might be invalid, together with its errors.
Deserializing a Validated never fails: the errors of the value are
kept in it instead of failing the deserialization of the value it’s in.
That covers the errors of the value itself (for instance a string where
a number is expected), of the values nested in it and the violation of
the validator V (none by default). Within the value, all errors are
collected (see State::set_collect_errors): the error holds all
problems of the value, not just the first one. Only the errors of the
data format and of layers (for instance
Limits) still fail the deserialization, and
the error that exceeds the limit of errors (see
State::set_max_errors).
use deser::Deserialize;
use deser_validate::{Email, Validated};
#[derive(Deserialize)]
struct Signup {
email: Validated<String, Email>,
age: Validated<u8>,
}
let signup: Signup =
deser_json::from_str(r#"{"email": "nope", "age": "x"}"#).unwrap();
assert!(!signup.email.is_valid());
assert_eq!(signup.email.unchecked_value().unwrap(), "nope");
assert_eq!(
signup.email.error().unwrap().to_string(),
"InvalidValue: invalid value: must be an email address at offset 10"
);
assert_eq!(signup.age.value(), None);The errors have the context of the value attached, like errors that
are returned (for instance the path with a
PathLayer). If the format provides the
source (see deser_location), their lines and columns are resolved.
If a Validation runs, they are reported to it
too.
While an untagged enum tries its variants, errors are not kept: a
variant with an invalid Validated value does not match.
use deser::Deserialize;
use deser_validate::Validated;
#[derive(Deserialize)]
struct Address {
street: String,
zip: u32,
}
#[derive(Deserialize)]
struct Order {
shipping: Validated<Address>,
}
let order: Order =
deser_json::from_str(r#"{"shipping": {"zip": "x"}}"#).unwrap();
let err = order.shipping.error().unwrap();
let errors: Vec<_> = err.errors().map(|err| err.message()).collect();
assert_eq!(
errors,
["unexpected string, expected u32", "missing field `street`"]
);When serialized, the value is serialized (also if it’s invalid), a value that could not be deserialized is serialized as null.
Implementations§
Source§impl<T, V> Validated<T, V>
impl<T, V> Validated<T, V>
Sourcepub fn from_error(error: Error) -> Validated<T, V>
pub fn from_error(error: Error) -> Validated<T, V>
Creates an invalid value from an error.
Sourcepub fn unchecked_value(&self) -> Option<&T>
pub fn unchecked_value(&self) -> Option<&T>
Returns the value, also if the validator rejected it.
Returns None if the value could not be deserialized.
Sourcepub fn error(&self) -> Option<&Error>
pub fn error(&self) -> Option<&Error>
Returns the error if the value is invalid.
The error holds all errors of the value (see Error::errors).
Sourcepub fn into_result(self) -> Result<T, Error>
pub fn into_result(self) -> Result<T, Error>
Returns the value if it’s valid and the error otherwise.
Trait Implementations§
Source§impl<'de, T: Deserialize<'de>, V: Validator<T>> Deserialize<'de> for Validated<T, V>
impl<'de, T: Deserialize<'de>, V: Validator<T>> Deserialize<'de> for Validated<T, V>
Source§fn initial_value() -> Option<Self>
fn initial_value() -> Option<Self>
Missing values are the missing values of T (validated).