Valida provides a powerful and flexible error reporting system that separates validation failures from system-level errors. Errors are structured, deeply nestable, serializable, and localization-ready (when i18n is enabled).
Represents a single error leaf. Includes:
key: String— the error code or i18n keyparams: HashMap<String, String>— optional arguments for localized message interpolation
Create errors with:
ValidationError::new("email.invalid")
ValidationError::new_with_params("validator.max_length", {
"max" => "100"
})Used internally to represent a tree of nested errors:
enum ValidationNode {
Leaf(ValidationError),
Branch(HashMap<String, ValidationNode>)
}The top-level container used throughout Valida:
- Stores errors as a tree keyed by field path
- Can add errors manually (
add(...)) or aggregate nested errors (add_nested(...)) - Implements
Error + Display
Valida offers multiple error formats for UI and debugging.
println!("{}", errors.pretty_print_raw())Outputs a tree-like view:
email: email.invalid
profile:
age: age.too_young
device:
name: validator.min_lengthlet json = errors.to_json_raw();Includes both key and params:
{
"email": {
"key": "email.invalid",
"params": {}
},
"profile": {
"age": {
"key": "age.too_young",
"params": {}
}
}
}let form = errors.to_json_form_raw();Produces flat keys like field[subfield]:
{
"profile[device][name]": {
"key": "validator.min_length",
"params": {}
}
}Perfect for HTML-based form rendering.
let dot = errors.to_json_dot_raw();Useful for config files or path mapping:
{
"profile.device.name": {
"key": "validator.min_length",
"params": {}
}
}When compiled with i18n-localization feature, Valida supports:
- pretty_print(locale)
- to_json(locale)
- to_json_form(locale)
- to_json_dot(locale)
Localization is powered by rust-i18n
Prefer using new_with_params(...) for dynamic error content
Use error keys that map directly to translation files (validator.min_length, etc.)
Nest errors via nested(...) for multi-layered DTOs