Errors

Formatting

flatten_error, treeify_error, prettify_error and Display.

The formatting helpers live in vld::error::format (also vld::format) and are in the prelude.

use vld::prelude::*;

vld::schema! {
    #[derive(Debug)]
    pub struct SignUp {
        pub name: String => vld::string().min(2),
        pub email: String => vld::string().email(),
    }
}

let err = SignUp::parse(r#"{"name": "A", "email": "nope"}"#).unwrap_err();

flatten_error

Groups messages by the top-level field, which is what most forms need. Issues without a path go to form_errors.

let flat = flatten_error(&err);
assert_eq!(flat.field_errors["name"], ["String must be at least 2 characters"]);
assert!(flat.form_errors.is_empty());

FlatError serializes to:

{
  "form_errors": [],
  "field_errors": {
    "name": ["String must be at least 2 characters"],
    "email": ["Invalid email address"]
  }
}

treeify_error

Builds a tree that mirrors the input, for nested forms: ErrorTree { errors, properties, items }.

let tree = treeify_error(&err);
assert_eq!(tree.properties["email"].errors, ["Invalid email address"]);

Array items are in items: Vec<Option<ErrorTree>> by index (None for items without errors).

prettify_error

A multi-line string for logs and CLIs:

✖ String must be at least 2 characters
  → at .name, received "A"
✖ Invalid email address
  → at .email, received "nope"

Display

err.to_string() gives one line per issue: .name: String must be at least 2 characters, received "A". See Error model.

Web responses

The web integration crates already turn a VldError into a 422 JSON body with message, path and code per issue. See the web overview.