#[non_exhaustive]pub struct Violation {
pub field: Option<&'static str>,
pub message: &'static str,
}Expand description
A single failed constraint, optionally associated with a named field.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.field: Option<&'static str>The field name, if validation was run on a named field.
message: &'static strA human-readable description of the constraint that failed.
Implementations§
Source§impl Violation
impl Violation
Sourcepub const fn with_field(field: &'static str, message: &'static str) -> Self
pub const fn with_field(field: &'static str, message: &'static str) -> Self
Creates a violation associated with a named field.
Examples found in repository?
examples/basic.rs (lines 23-26)
19 fn validate(&self) -> Result<(), Self::Error> {
20 let mut errors = ValidationError::empty();
21
22 if self.username.len() < 3 {
23 errors.push(Violation::with_field(
24 "username",
25 "must be at least 3 characters",
26 ));
27 }
28 if self.age < 18 {
29 errors.push(Violation::with_field("age", "must be 18 or older"));
30 }
31 if !self.email.contains('@') {
32 errors.push(Violation::with_field("email", "must contain '@'"));
33 }
34
35 if errors.is_empty() {
36 Ok(())
37 } else {
38 Err(errors)
39 }
40 }Trait Implementations§
impl Eq for Violation
Source§impl Extend<Violation> for ValidationError
Available on crate feature alloc only.
impl Extend<Violation> for ValidationError
Available on crate feature
alloc only.Source§fn extend<I: IntoIterator<Item = Violation>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = Violation>>(&mut self, iter: I)
Extends a collection with the contents of an iterator. Read more
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one)Extends a collection with exactly one element.
Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one)Reserves capacity in a collection for the given number of additional elements. Read more
Source§impl From<Violation> for ValidationError
Available on crate feature alloc only.
impl From<Violation> for ValidationError
Available on crate feature
alloc only.Source§impl FromIterator<Violation> for ValidationError
Available on crate feature alloc only.
impl FromIterator<Violation> for ValidationError
Available on crate feature
alloc only.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