diff options
| -rw-r--r-- | validator/src/types.rs | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/validator/src/types.rs b/validator/src/types.rs index 5b96cfe..0a7c2fa 100644 --- a/validator/src/types.rs +++ b/validator/src/types.rs @@ -1,6 +1,6 @@ +use std::{self, fmt}; use std::collections::HashMap; - #[derive(Debug)] pub struct Errors(HashMap<String, Vec<String>>); @@ -22,6 +22,33 @@ impl Errors { } } +impl fmt::Display for Errors { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "Validation failed:\n")?; + for (field, errs) in &self.0 { + write!(fmt, " {}: [", field)?; + + let last = errs.len() - 1; + for (index, err) in errs.iter().enumerate() { + write!(fmt, "{}", err)?; + if index < last { write!(fmt, ", ")? } + } + write!(fmt, "]\n")?; + } + Ok(()) + } +} + +impl std::error::Error for Errors { + fn description(&self) -> &str { + "validation failed" + } + + fn cause(&self) -> Option<&std::error::Error> { + None + } +} + pub trait Validate { fn validate(&self) -> Result<(), Errors>; } |
