pub struct ValidationErrors {
pub errors: Vec<ValidationError>,
pub fields: BTreeMap<String, FieldErrors>,
}Expand description
Container for validation errors with nested structure support.
§Example
use skp_validator_core::{ValidationErrors, ValidationError};
let mut errors = ValidationErrors::new();
errors.add_field_error("email", ValidationError::new("email", "email.invalid", "Invalid email"));
errors.add_field_error("name", ValidationError::new("name", "required", "Name is required"));
assert_eq!(errors.count(), 2);Fields§
§errors: Vec<ValidationError>Root-level errors (struct-wide validations)
fields: BTreeMap<String, FieldErrors>Field-level errors
Implementations§
Source§impl ValidationErrors
impl ValidationErrors
Sourcepub fn new() -> ValidationErrors
pub fn new() -> ValidationErrors
Create an empty error container
Sourcepub fn from_error(error: ValidationError) -> ValidationErrors
pub fn from_error(error: ValidationError) -> ValidationErrors
Create from a single error
Sourcepub fn add_root_error(&mut self, error: ValidationError)
pub fn add_root_error(&mut self, error: ValidationError)
Add a root-level error (not associated with a specific field)
Sourcepub fn add_field_error(
&mut self,
field: impl Into<String>,
error: ValidationError,
)
pub fn add_field_error( &mut self, field: impl Into<String>, error: ValidationError, )
Add a field-level error
Sourcepub fn add_nested_errors(
&mut self,
field: impl Into<String>,
errors: ValidationErrors,
)
pub fn add_nested_errors( &mut self, field: impl Into<String>, errors: ValidationErrors, )
Add nested struct errors
Sourcepub fn add_list_errors(
&mut self,
field: impl Into<String>,
errors: BTreeMap<usize, Box<ValidationErrors>>,
)
pub fn add_list_errors( &mut self, field: impl Into<String>, errors: BTreeMap<usize, Box<ValidationErrors>>, )
Add list item errors
Sourcepub fn add_map_errors(
&mut self,
field: impl Into<String>,
errors: BTreeMap<String, Box<ValidationErrors>>,
)
pub fn add_map_errors( &mut self, field: impl Into<String>, errors: BTreeMap<String, Box<ValidationErrors>>, )
Add map item errors
Sourcepub fn merge(&mut self, other: ValidationErrors)
pub fn merge(&mut self, other: ValidationErrors)
Merge another ValidationErrors into this one
Sourcepub fn merge_result<T>(
&mut self,
result: Result<T, ValidationErrors>,
) -> Option<T>
pub fn merge_result<T>( &mut self, result: Result<T, ValidationErrors>, ) -> Option<T>
Merge errors from a Result
Sourcepub fn to_flat_map(&self) -> BTreeMap<String, Vec<String>>
pub fn to_flat_map(&self) -> BTreeMap<String, Vec<String>>
Convert to a flat map format: “path” -> [“error1”, “error2”]
Useful for simpler error handling or compatibility with other formats.
Sourcepub fn field(&self, name: &str) -> Option<&FieldErrors>
pub fn field(&self, name: &str) -> Option<&FieldErrors>
Get errors for a specific field
Sourcepub fn has_field_error(&self, name: &str) -> bool
pub fn has_field_error(&self, name: &str) -> bool
Check if a specific field has errors
Sourcepub fn field_count(&self) -> usize
pub fn field_count(&self) -> usize
Get the number of fields with errors
Sourcepub fn field_errors(&self) -> impl Iterator<Item = (&String, &FieldErrors)>
pub fn field_errors(&self) -> impl Iterator<Item = (&String, &FieldErrors)>
Iterate over field errors
Sourcepub fn to_json_string(&self) -> String
pub fn to_json_string(&self) -> String
Convert to JSON string (requires serde feature)
Sourcepub fn to_json_pretty(&self) -> String
pub fn to_json_pretty(&self) -> String
Convert to pretty JSON string (requires serde feature)
Trait Implementations§
Source§impl Clone for ValidationErrors
impl Clone for ValidationErrors
Source§fn clone(&self) -> ValidationErrors
fn clone(&self) -> ValidationErrors
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more