all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Dietmar Maurer <dietmar@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox] ParameterError: construct XPath like string to identify nested properties
Date: Tue, 28 Sep 2021 12:39:04 +0200	[thread overview]
Message-ID: <20210928103904.4058186-1-dietmar@proxmox.com> (raw)

---
 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





                 reply	other threads:[~2021-09-28 10:39 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210928103904.4058186-1-dietmar@proxmox.com \
    --to=dietmar@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal