Understand ValidationErrorsKind and error structures
masterWhen validation fails, validate() returns ValidationErrors. The errors are categorized by the ValidationErrorsKind enum, which determines how errors are nested:
Struct(Box<ValidationErrors>): Used for errors found in a single nested struct (via#[validate(nested)]).List(BTreeMap<usize, Box<ValidationErrors>>): Used for errors found in a vector of nested structs, keyed by the index of the invalid entry.Field(Vec<ValidationError>): Used for simple field-level errors.
A ValidationError contains:
code: ACow<'static, str>representing the error type.message: An optionalCow<'static, str>describing the error.params: AHashMap<Cow<'static, str>, Value>containing metadata, including the field'svalueautomatically.
#[derive(Debug, Serialize, Clone, PartialEq)]
#[serde(untagged)]
pub enum ValidationErrorsKind {
Struct(Box<ValidationErrors>),
List(BTreeMap<usize, Box<ValidationErrors>>),
Field(Vec<ValidationError>),
}
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct ValidationError {
pub code: Cow<'static, str>,
pub message: Option<Cow<'static, str>>,
pub params: HashMap<Cow<'static, str>, Value>,
}