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§message: StringImplementations§
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§
Source§impl Debug for ValidationError
impl Debug for ValidationError
Source§impl Diagnostic for ValidationError
impl Diagnostic for ValidationError
Source§fn help(&self) -> Option<Box<dyn Display + '_>>
fn help(&self) -> Option<Box<dyn Display + '_>>
Additional help text related to this
Diagnostic. Do you have any
advice for the poor soul who’s just run into this issue?Source§fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>>
fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>>
Unique diagnostic code that can be used to look up more information
about this
Diagnostic. Ideally also globally unique, and documented
in the toplevel crate’s documentation for easy searching. Rust path
format (foo::bar::baz) is recommended, but more classic codes like
E0123 or enums will work just fine.Source§fn severity(&self) -> Option<Severity>
fn severity(&self) -> Option<Severity>
Diagnostic severity. This may be used by
ReportHandlers to change the display format
of this diagnostic. Read moreSource§fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>>
fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>>
URL to visit for a more detailed explanation/help about this
Diagnostic.Source§fn source_code(&self) -> Option<&dyn SourceCode>
fn source_code(&self) -> Option<&dyn SourceCode>
Source code to apply this
Diagnostic’s Diagnostic::labels to.Source§fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>>
fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>>
Labels to apply to this
Diagnostic’s Diagnostic::source_codeAdditional related
Diagnostics.Source§fn diagnostic_source(&self) -> Option<&dyn Diagnostic>
fn diagnostic_source(&self) -> Option<&dyn Diagnostic>
The cause of the error.
Source§impl Display for ValidationError
impl Display for ValidationError
Source§impl Error for ValidationError
impl Error for ValidationError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
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