From 21937db8dec0f05d35a37114f0259b0c9753ff26 Mon Sep 17 00:00:00 2001 From: Alex Groleau Date: Wed, 16 Apr 2025 14:42:57 -0400 Subject: [PATCH] improved compile schema error messages --- .env | 4 ++-- src/lib.rs | 24 +++++++++++++----------- src/tests.rs | 36 +++++++++++++++++------------------- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/.env b/.env index 265acc8..a977737 100644 --- a/.env +++ b/.env @@ -1,7 +1,7 @@ ENVIRONMENT=local -DATABASE_PASSWORD=QgSvstSjoc6fKphMzNgT3SliNY10eSRS +DATABASE_PASSWORD=tIr4TJ0qUwGVM0rlQSe3W7Tgpi33zPbk DATABASE_ROLE=agreego_admin -DATABASE_HOST=127.1.27.9 +DATABASE_HOST=127.1.27.4 DATABASE_PORT=5432 POSTGRES_PASSWORD=xzIq5JT0xY3F+2m1GtnrKDdK29sNSXVVYZHPKJVh8pI= DATABASE_NAME=agreego diff --git a/src/lib.rs b/src/lib.rs index 1375819..8f95b3a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,22 +48,24 @@ fn cache_json_schema(schema_id: &str, schema: JsonB) -> JsonB { } Err(e) => { let error = match &e { - CompileError::ValidationError { url: _url, src } => { // Prefix url with _ - json!({ - "message": format!("Schema '{}' failed validation against its metaschema: {}", schema_id, src), - "schema_path": schema_path, - "error": format!("{:?}", src), - }) + CompileError::ValidationError { url: _url, src } => { + // Collect leaf errors from the meta-schema validation failure + let mut error_list = Vec::new(); + collect_leaf_errors(src, &mut error_list); + // Return the flat list directly + json!(error_list) } _ => { - let _error_type = format!("{:?}", e).split('(').next().unwrap_or("Unknown").to_string(); // Prefix error_type with _ + // Keep existing handling for other compilation errors + let _error_type = format!("{:?}", e).split('(').next().unwrap_or("Unknown").to_string(); json!({ "message": format!("Schema '{}' compilation failed: {}", schema_id, e), "schema_path": schema_path, - "error": format!("{:?}", e), + "detail": format!("{:?}", e), }) } }; + // Ensure the outer structure remains { success: false, error: ... } JsonB(json!({ "success": false, "error": error @@ -91,11 +93,11 @@ 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 - let mut all_errors = Vec::new(); - collect_leaf_errors(&validation_error, &mut all_errors); + let mut error_list = Vec::new(); + collect_leaf_errors(&validation_error, &mut error_list); JsonB(json!({ "success": false, - "error": all_errors // Flat list of specific errors + "error": error_list // Flat list of specific errors })) } } diff --git a/src/tests.rs b/src/tests.rs index 7e2431e..e0ff638 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -251,26 +251,24 @@ fn test_cache_invalid_json_schema() { let cache_result = cache_json_schema(schema_id, jsonb(invalid_schema)); - // Manually check the structure for cache_json_schema failure - let json_result = &cache_result.0; - let success = json_result.get("success").and_then(Value::as_bool); - let error_obj = json_result.get("error").and_then(Value::as_object); + // Expect 2 leaf errors because the meta-schema validation fails at the type value + // and within the type array itself. + assert_failure_with_json!( + cache_result, + 2, // Expect exactly two leaf errors + "value must be one of", // Check message substring (present in both) + "Caching invalid schema should fail with specific meta-schema validation errors." + ); - if success != Some(false) { - let pretty_json = serde_json::to_string_pretty(&json_result).unwrap_or_else(|_| format!("(Failed to pretty-print JSON: {:?})", json_result)); - panic!("Assertion Failed (expected failure, success was not false): Caching invalid schema should fail.\nResult JSON:\n{}", pretty_json); - } - if error_obj.is_none() { - let pretty_json = serde_json::to_string_pretty(&json_result).unwrap_or_else(|_| format!("(Failed to pretty-print JSON: {:?})", json_result)); - panic!("Assertion Failed (expected 'error' object, but none found): Caching invalid schema should return an error object.\nResult JSON:\n{}", pretty_json); - } - // Check specific fields within the error object - let message = error_obj.unwrap().get("message").and_then(Value::as_str); - // Updated check based on the actual error message seen in the logs - if message.map_or(true, |m| !m.contains("failed validation against its metaschema") || !m.contains("/type/0': value must be one of")) { - let pretty_json = serde_json::to_string_pretty(&json_result).unwrap_or_else(|_| format!("(Failed to pretty-print JSON: {:?})", json_result)); - panic!("Assertion Failed (error message mismatch): Expected metaschema validation failure message containing '/type/0' error detail.\nResult JSON:\n{}", pretty_json); - } + // Ensure the error is an array and check specifics + let error_array = cache_result.0["error"].as_array().expect("Error field should be an array"); + assert_eq!(error_array.len(), 2); + // Note: Order might vary depending on boon's internal processing, check both possibilities or sort. + // Assuming the order shown in the logs for now: + assert_eq!(error_array[0]["instance_path"], "/type"); + assert!(error_array[0]["message"].as_str().unwrap().contains("value must be one of")); + assert_eq!(error_array[1]["instance_path"], "/type/0"); + assert!(error_array[1]["message"].as_str().unwrap().contains("value must be one of")); } #[pg_test]