* [pbs-devel] [PATCH proxmox] ParameterError: construct XPath like string to identify nested properties
@ 2021-09-28 10:39 Dietmar Maurer
0 siblings, 0 replies; only message in thread
From: Dietmar Maurer @ 2021-09-28 10:39 UTC (permalink / raw)
To: pbs-devel
---
proxmox/src/api/schema.rs | 23 +++++++++++++++++++----
1 file changed, 19 insertions(+), 4 deletions(-)
diff --git a/proxmox/src/api/schema.rs b/proxmox/src/api/schema.rs
index 10e84f7..1d2000f 100644
--- a/proxmox/src/api/schema.rs
+++ b/proxmox/src/api/schema.rs
@@ -46,6 +46,17 @@ impl ParameterError {
pub fn is_empty(&self) -> bool {
self.len() == 0
}
+
+ pub fn add_errors(&mut self, prefix: &str, err: Error) {
+ if let Some(param_err) = err.downcast_ref::<ParameterError>() {
+ for (sub_key, sub_err) in param_err.errors().iter() {
+ self.push(format!("{}/{}", prefix, sub_key), format_err!("{}", sub_err));
+ }
+ } else {
+ self.push(prefix.to_string(), err);
+ }
+ }
+
}
impl fmt::Display for ParameterError {
@@ -1079,8 +1090,13 @@ pub fn verify_json_array(data: &Value, schema: &ArraySchema) -> Result<(), Error
schema.check_length(list.len())?;
- for item in list {
- verify_json(item, &schema.items)?;
+ for (i, item) in list.iter().enumerate() {
+ let result = verify_json(item, &schema.items);
+ if let Err(err) = result {
+ let mut errors = ParameterError::new();
+ errors.add_errors(&format!("[{}]", i), err);
+ return Err(errors.into());
+ }
}
Ok(())
@@ -1097,7 +1113,6 @@ pub fn verify_json_object(
_ => bail!("Expected object - got scalar value."),
};
- // fixme: improve error messages from nested objects/arrays
let mut errors = ParameterError::new();
let additional_properties = schema.additional_properties();
@@ -1110,7 +1125,7 @@ pub fn verify_json_object(
_ => verify_json(value, prop_schema),
};
if let Err(err) = result {
- errors.push(key.to_string(), err);
+ errors.add_errors(key, err);
};
} else if !additional_properties {
errors.push(key.to_string(), format_err!("schema does not allow additional properties."));
--
2.30.2
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2021-09-28 10:39 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-28 10:39 [pbs-devel] [PATCH proxmox] ParameterError: construct XPath like string to identify nested properties Dietmar Maurer
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.