pub struct ValidationError {
pub field: String,
pub message: String,
}
Expand description
Represents an error that occurs during validation of a struct’s field.
Each ValidationError
contains the name of the field that failed validation
and an associated error message explaining the reason for the failure.
Fields§
§field: String
The name of the field that failed validation.
message: String
A message describing the validation error.
Implementations§
Source§impl ValidationError
impl ValidationError
Sourcepub fn new(field: &str, message: &str) -> Self
pub fn new(field: &str, message: &str) -> Self
Creates a new ValidationError
for a specific field with a given message.
§Arguments
field
- The name of the field that failed validation.message
- A description of the validation error.
§Examples
use struct_validation_core::ValidationError;
let error = ValidationError::new("username", "must not be empty");
assert_eq!(error.field, "username");
assert_eq!(error.message, "must not be empty");
Sourcepub fn add_prefix(&mut self, prefix: &str)
pub fn add_prefix(&mut self, prefix: &str)
Adds a prefix to the field name, separated by a dot.
This can be useful for nested validation errors where the field is part of a larger struct.
§Arguments
prefix
- The prefix to add to the field name.
§Examples
use struct_validation_core::ValidationError;
let mut error = ValidationError::new("username", "must not be empty");
error.add_prefix("user");
assert_eq!(error.field, "user.username");
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ValidationError
impl RefUnwindSafe for ValidationError
impl Send for ValidationError
impl Sync for ValidationError
impl Unpin for ValidationError
impl UnwindSafe for ValidationError
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