made errors consistent

This commit is contained in:
2025-04-16 01:00:51 -04:00
parent 3ceb8a0770
commit cc04a1a8bb
2 changed files with 98 additions and 64 deletions

View File

@ -80,11 +80,9 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
match cache.id_to_index.get(schema_id) {
None => JsonB(json!({
"success": false,
"errors": [json!({
"message": format!("Schema with id '{}' not found in cache", schema_id),
"schema_path": "",
"instance_path": ""
})]
"error": {
"message": format!("Schema with id '{}' not found in cache", schema_id)
}
})),
Some(sch_index) => {
let instance_value: Value = instance.0;
@ -93,7 +91,12 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
Err(validation_error) => {
// Directly use the result of format_validation_error
// which now includes the top-level success indicator and flat error list
JsonB(format_validation_error(&validation_error))
let mut all_errors = Vec::new();
collect_leaf_errors(&validation_error, &mut all_errors);
JsonB(json!({
"success": false,
"error": all_errors // Flat list of specific errors
}))
}
}
}
@ -111,28 +114,17 @@ fn collect_leaf_errors(error: &ValidationError, errors_list: &mut Vec<Value>) {
};
errors_list.push(json!({
"message": message,
"schema_path": error.schema_url.to_string(),
"instance_path": error.instance_location.to_string(),
"message": message,
"schema_path": error.schema_url.to_string(),
"instance_path": error.instance_location.to_string(),
}));
} else {
for cause in &error.causes {
collect_leaf_errors(cause, errors_list);
collect_leaf_errors(cause, errors_list);
}
}
}
// Formats validation errors into a flat list JSON structure
fn format_validation_error(error: &ValidationError) -> Value {
let mut all_errors = Vec::new();
collect_leaf_errors(error, &mut all_errors);
json!({
"success": false,
"errors": all_errors // Flat list of specific errors
})
}
#[pg_extern(strict, parallel_safe)]
fn json_schema_cached(schema_id: &str) -> bool {
let cache = SCHEMA_CACHE.read().unwrap();