pub struct ValidationError {
pub violations: SmallVec<[Violation; 4]>,
}Expand description
Represents a collection of validation violations.
This type aggregates all validation errors that occur during validation of a domain object. It supports merging errors from multiple fields and nested structures.
§Examples
use domainstack::{ValidationError, Path};
let mut err = ValidationError::new();
err.push("email", "invalid_email", "Invalid email format");
err.push("age", "out_of_range", "Must be between 18 and 120");
assert_eq!(err.violations.len(), 2);
assert!(!err.is_empty());§Nested Errors
use domainstack::{ValidationError, Path};
let mut child_err = ValidationError::new();
child_err.push("value", "invalid_email", "Invalid email format");
let mut parent_err = ValidationError::new();
parent_err.merge_prefixed("email", child_err);
// Error path becomes "email.value"
assert_eq!(parent_err.violations[0].path.to_string(), "email.value");Fields§
§violations: SmallVec<[Violation; 4]>Implementations§
Source§impl ValidationError
impl ValidationError
pub fn new() -> Self
pub fn is_empty(&self) -> bool
pub fn single( path: impl Into<Path>, code: &'static str, message: impl Into<String>, ) -> Self
pub fn push( &mut self, path: impl Into<Path>, code: &'static str, message: impl Into<String>, )
pub fn extend(&mut self, other: ValidationError)
Sourcepub fn merge_prefixed(
&mut self,
prefix: impl Into<Path>,
other: ValidationError,
)
pub fn merge_prefixed( &mut self, prefix: impl Into<Path>, other: ValidationError, )
Merges violations from another error with a path prefix.
This is optimized to avoid cloning the prefix for each violation. The prefix segments are collected once and reused for all violations.
§Performance
- Old: O(n) prefix clones where n = number of violations
- New: O(1) prefix collection + O(n * m) segment clones where m = avg segments per path
For typical use cases (< 10 violations), this optimization reduces allocations significantly.
Sourcepub fn field_errors_map(&self) -> BTreeMap<String, Vec<String>>
👎Deprecated since 0.5.0: Use field_violations_map() instead to preserve error codes and metadata. This method only returns messages and loses important error information.
pub fn field_errors_map(&self) -> BTreeMap<String, Vec<String>>
field_violations_map() instead to preserve error codes and metadata. This method only returns messages and loses important error information.Returns a map of field paths to error messages.
⚠️ Warning: This method only returns messages and loses error codes and metadata.
For complete error information including codes (needed for proper error classification,
client-side handling, and internationalization), use field_violations_map() instead.
§Examples
use domainstack::ValidationError;
let mut err = ValidationError::new();
err.push("email", "invalid_format", "Invalid email format");
err.push("email", "too_long", "Email too long");
let map = err.field_errors_map();
assert_eq!(map.get("email").unwrap().len(), 2);
// Note: Error codes "invalid_format" and "too_long" are lost!pub fn field_violations_map(&self) -> BTreeMap<String, Vec<&Violation>>
Sourcepub fn prefixed(self, prefix: impl Into<Path>) -> Self
pub fn prefixed(self, prefix: impl Into<Path>) -> Self
Returns a new ValidationError with all paths prefixed.
§Performance
This method is optimized to collect prefix segments once and reuse them, avoiding repeated cloning of the prefix path.
Sourcepub fn map_messages<F>(self, f: F) -> Self
pub fn map_messages<F>(self, f: F) -> Self
Transform all violation messages using the provided function.
This is useful for internationalization (i18n), message formatting, or any other message transformation needs.
§Examples
use domainstack::ValidationError;
let mut err = ValidationError::new();
err.push("email", "invalid", "Invalid email");
err.push("age", "too_young", "Must be 18+");
// Add prefix to all messages
let err = err.map_messages(|msg| format!("Error: {}", msg));
assert_eq!(err.violations[0].message, "Error: Invalid email");
assert_eq!(err.violations[1].message, "Error: Must be 18+");§Internationalization Example
use domainstack::ValidationError;
let mut err = ValidationError::new();
err.push("email", "invalid_email", "Invalid email format");
// Translate messages based on error code
let err = err.map_messages(|msg| {
// In a real app, this would use error codes for translation
"Formato de email inválido".to_string()
});
assert_eq!(err.violations[0].message, "Formato de email inválido");Sourcepub fn filter<F>(self, f: F) -> Self
pub fn filter<F>(self, f: F) -> Self
Filter violations based on a predicate.
This is useful for removing certain types of errors based on custom criteria.
§Examples
use domainstack::ValidationError;
let mut err = ValidationError::new();
err.push("email", "warning", "Email format questionable");
err.push("age", "invalid", "Age is required");
err.push("name", "warning", "Name seems unusual");
// Remove all warnings, keep only errors
let err = err.filter(|v| v.code != "warning");
assert_eq!(err.violations.len(), 1);
assert_eq!(err.violations[0].code, "invalid");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 more