pub struct Violation { /* private fields */ }Expand description
Why a value is invalid.
A violation has a code which identifies the rule that was violated (for
instance length), a message for humans and parameters (for instance
the minimum length). Codes and parameters are meant for programs, for
instance to translate the messages or to point at the value in a user
interface.
When a value is invalid, the error has the violation attached (see
Error::attachment):
use deser::Deserialize;
use deser_validate::{Check, Email, Violation};
#[derive(Deserialize, Debug)]
struct User {
#[deser(as = Check<Email>)]
email: String,
}
let json = r#"{"email": "nope"}"#;
let err = deser_json::from_str::<User>(json).unwrap_err();
assert_eq!(
err.to_string(),
"InvalidValue: invalid value: must be an email address \
at line 1 column 11"
);
assert_eq!(err.attachment::<Violation>().unwrap().code(), "email");Implementations§
Source§impl Violation
impl Violation
Sourcepub fn new<C, M>(code: C, message: M) -> Violation
pub fn new<C, M>(code: C, message: M) -> Violation
Creates a violation with a code and a message.
Sourcepub fn with_param<P: Into<Param>>(
self,
name: &'static str,
value: P,
) -> Violation
pub fn with_param<P: Into<Param>>( self, name: &'static str, value: P, ) -> Violation
Adds a parameter.
Sourcepub fn into_error(self) -> Error
pub fn into_error(self) -> Error
Creates the error for the violation.
The error has the violation attached, the message is the same as
the one of #[deser(validate = ...)].
Trait Implementations§
Source§impl ErrorAttachment for Violation
impl ErrorAttachment for Violation
Source§impl IntoViolation for Violation
impl IntoViolation for Violation
impl StructuralPartialEq for Violation
Auto Trait Implementations§
impl Freeze for Violation
impl RefUnwindSafe for Violation
impl Send for Violation
impl Sync for Violation
impl Unpin for Violation
impl UnsafeUnpin for Violation
impl UnwindSafe for Violation
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