Skip to content

Commit 571adff

Browse files
chore: Better JSON validation error messages
* chore: Better JSON validation error messages Signed-off-by: aish-where-ya <aharpale@cosmonic.com> * nit fix Signed-off-by: aish-where-ya <aharpale@cosmonic.com> --------- Signed-off-by: aish-where-ya <aharpale@cosmonic.com>
1 parent f628a17 commit 571adff

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/server/handlers.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet};
33
use anyhow::anyhow;
44
use async_nats::{jetstream::stream::Stream, Client, Message};
55
use base64::{engine::general_purpose::STANDARD as B64decoder, Engine};
6-
use jsonschema::{Draft, JSONSchema};
6+
use jsonschema::{paths::PathChunk, Draft, JSONSchema};
77
use regex::Regex;
88
use serde_json::json;
99
use tokio::sync::OnceCell;
@@ -952,14 +952,26 @@ pub(crate) async fn validate_manifest(manifest: Manifest) -> anyhow::Result<()>
952952
if let Err(errors) = validation_result {
953953
let mut error_message = String::new();
954954
for error in errors {
955+
let instance_path = error
956+
.instance_path
957+
.into_iter()
958+
.map(|item| match item {
959+
PathChunk::Property(value) => value.to_string(),
960+
PathChunk::Index(idx) => format!(" at index: {idx}"),
961+
PathChunk::Keyword(keyword) => keyword.to_string(),
962+
})
963+
.collect::<Vec<String>>()
964+
.join("/");
955965
error_message.push_str(&format!(
956-
"Validation error in object: {} \nObject path: {}",
957-
// Error instance in the JSON instance and its corresponding path in that file
958-
error.instance,
959-
error.instance_path
966+
"Should be able to parse object at: {} \n",
967+
// The path of the corresponding JSON error instance in that file
968+
instance_path
960969
));
961970
}
962-
return Err(anyhow!("Validation Error : \n{}", error_message));
971+
return Err(anyhow!(
972+
"Validation Error: \n{}Please check for missing or incorrect elements",
973+
error_message
974+
));
963975
}
964976

965977
// Map of link names to a vector of provider references with that link name
@@ -1122,7 +1134,7 @@ mod test {
11221134
assert!(e
11231135
.to_string()
11241136
// The 0th component in the spec list is incorrect and should be detected (indexing starts from 0)
1125-
.contains("Object path: /spec/components/0"))
1137+
.contains("Should be able to parse object at: spec/components/ at index: 0"))
11261138
}
11271139
}
11281140

0 commit comments

Comments
 (0)