pub struct ValidationError {
pub type_name: &'static str,
pub reason: String,
}Expand description
A validation failure for a domain value, naming the type that rejected the value and why.
type_name is a plain string, not tied to Self by the compiler: it is
whatever the caller of ValidationError::new passes in. Derive-macro
generated code always passes the exact type name it was expanded for, so
it is guaranteed accurate there. A hand-written impl (as in this
crate’s own test fixtures) must keep the type_name argument in sync
with the type by hand - copy-pasting an impl from one type to another is
the most likely way for it to drift.
With the serde feature enabled, this derives Serialize only, not
Deserialize: type_name is &'static str, and there is no sound way
to produce a &'static str from arbitrary deserialized input without
leaking memory. Serializing is the common need anyway (e.g. reporting a
validation failure in an API error body); reconstructing one from JSON
is not.
§Examples
use ddd_toolkit_core::domain::ValidationError;
let error = ValidationError::new("Email", "missing '@'");
assert_eq!(error.type_name, "Email");
assert_eq!(error.reason, "missing '@'");
assert_eq!(error.to_string(), "invalid Email: missing '@'");Fields§
§type_name: &'static strThe name of the type that rejected the value.
reason: StringA human-readable explanation of why the value was rejected.
Implementations§
Trait Implementations§
Source§impl Clone for ValidationError
impl Clone for ValidationError
Source§impl Debug for ValidationError
impl Debug for ValidationError
Source§impl Display for ValidationError
impl Display for ValidationError
impl Eq for ValidationError
Source§impl Error for ValidationError
impl Error for ValidationError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()