serde_valid/validation/error/
object_errors.rs1use serde::ser::SerializeStruct;
2
3use super::{PropertyErrorsMap, VecErrors};
4
5#[derive(Debug, Clone, thiserror::Error)]
6pub struct ObjectErrors<E = crate::validation::Error> {
7 pub errors: VecErrors<E>,
8 pub properties: PropertyErrorsMap<E>,
9}
10
11impl<E> serde::Serialize for ObjectErrors<E>
12where
13 E: serde::Serialize,
14{
15 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
16 where
17 S: serde::Serializer,
18 {
19 let mut object_errors = serializer.serialize_struct("ObjectErrors", 2)?;
20 object_errors.serialize_field("errors", &self.errors)?;
21 object_errors.serialize_field("properties", &self.properties)?;
22 object_errors.end()
23 }
24}
25
26impl<E> ObjectErrors<E> {
27 pub fn new(errors: VecErrors<E>, properties: PropertyErrorsMap<E>) -> Self {
28 Self { errors, properties }
29 }
30}
31
32impl<E> std::fmt::Display for ObjectErrors<E>
33where
34 E: std::fmt::Display + serde::Serialize,
35{
36 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37 match serde_json::to_string(&self) {
38 Ok(json_string) => {
39 write!(f, "{}", json_string)
40 }
41 Err(_) => Err(std::fmt::Error),
42 }
43 }
44}