pub struct ValidationError {
pub path: Vec<PathSegment>,
pub code: String,
pub message: String,
pub params: HashMap<String, Value>,
}Expand description
A single validation error with location context.
Each error includes the path to the invalid field (supporting nested structs and collection indices), a machine-readable error code, a human-readable message, and optional constraint parameters for structured error reporting.
§Example
use rusdantic_core::{ValidationError, PathSegment};
let error = ValidationError {
path: vec![
PathSegment::Field("user".to_string()),
PathSegment::Field("email".to_string()),
],
code: "email".to_string(),
message: "invalid email format".to_string(),
params: Default::default(),
};
assert_eq!(error.path_string(), "user.email");Fields§
§path: Vec<PathSegment>Path to the invalid field.
For nested structs, this contains multiple segments:
["user", "addresses", 0, "zip_code"]
code: StringMachine-readable error code.
Standard codes include: "length_min", "length_max", "range_min",
"range_max", "email", "url", "pattern", "contains", "required",
"custom".
message: StringHuman-readable error message suitable for display to end users.
params: HashMap<String, Value>Constraint parameters providing context about the validation failure.
For example, a length violation might include:
{"min": 3, "max": 20, "actual": 1}
Implementations§
Source§impl ValidationError
impl ValidationError
Sourcepub fn new(code: impl Into<String>, message: impl Into<String>) -> Self
pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self
Create a new validation error with the given code and message.
Sourcepub fn with_param(self, key: impl Into<String>, value: impl Into<Value>) -> Self
pub fn with_param(self, key: impl Into<String>, value: impl Into<Value>) -> Self
Add a parameter to this error for structured error reporting.
Sourcepub fn with_path(self, path: Vec<PathSegment>) -> Self
pub fn with_path(self, path: Vec<PathSegment>) -> Self
Set the path for this error.
Sourcepub fn path_string(&self) -> String
pub fn path_string(&self) -> String
Format the path as a dot-notation string.
Field segments are joined with ., index segments are formatted as [N].
Example: "user.addresses[0].zip_code"
Trait Implementations§
Source§impl Clone for ValidationError
impl Clone for ValidationError
Source§fn clone(&self) -> ValidationError
fn clone(&self) -> ValidationError
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ValidationError
impl Debug for ValidationError
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)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Source§impl PartialEq for ValidationError
impl PartialEq for ValidationError
Source§impl Serialize for ValidationError
impl Serialize for ValidationError
impl StructuralPartialEq for ValidationError
Auto Trait Implementations§
impl Freeze for ValidationError
impl RefUnwindSafe for ValidationError
impl Send for ValidationError
impl Sync for ValidationError
impl Unpin for ValidationError
impl UnsafeUnpin 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Dump for Twhere
T: Serialize,
impl<T> Dump for Twhere
T: Serialize,
Source§fn dump_with(&self, options: &DumpOptions) -> Result<Value, Error>
fn dump_with(&self, options: &DumpOptions) -> Result<Value, Error>
serde_json::Value with custom options.