more error cleanup

This commit is contained in:
2025-04-16 00:38:04 -04:00
parent 6ca00f27e9
commit 499bf68b2a
2 changed files with 251 additions and 146 deletions

View File

@ -80,47 +80,56 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
match cache.id_to_index.get(schema_id) {
None => JsonB(json!({
"success": false,
"error": {
"message": format!("Schema with id '{}' not found in cache", schema_id)
}
"errors": [json!({
"message": format!("Schema with id '{}' not found in cache", schema_id),
"schema_path": "",
"instance_path": ""
})]
})),
Some(sch_index) => {
let instance_value: Value = instance.0;
match cache.schemas.validate(&instance_value, *sch_index) {
Ok(_) => JsonB(json!({ "success": true })),
Err(validation_error) => {
let error = format_validation_error(&validation_error);
JsonB(json!({
"success": false,
"error": 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))
}
}
}
}
}
fn format_validation_error(error: &ValidationError) -> Value {
let nested_errors: Vec<Value> = error.causes.iter().map(format_validation_error).collect();
// Use specific message for leaf errors, generic for containers
let message = if error.causes.is_empty() {
let default_message = format!("{}", error); // Use boon's default message for specific errors
// Try to strip the "at '<path>': " prefix
if let Some(start_index) = default_message.find("': ") {
// Recursively collects leaf errors into a flat list
fn collect_leaf_errors(error: &ValidationError, errors_list: &mut Vec<Value>) {
if error.causes.is_empty() {
let default_message = format!("{}", error);
let message = if let Some(start_index) = default_message.find("': ") {
default_message[start_index + 3..].to_string()
} else {
default_message // Fallback if pattern not found
}
} else {
"Validation failed due to nested errors".to_string() // Generic message for container errors
default_message
};
json!({
errors_list.push(json!({
"message": message,
"schema_path": error.schema_url.to_string(), // Use schema_url directly
"schema_path": error.schema_url.to_string(),
"instance_path": error.instance_location.to_string(),
"error": nested_errors // Recursively format nested errors
}));
} else {
for cause in &error.causes {
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
})
}

View File

@ -1,7 +1,8 @@
use crate::*;
use serde_json::{json, Value};
use pgrx::{JsonB, pg_test};
// Helper macro for asserting success with pretty JSON output on failure
// Helper macro for asserting success (no changes needed, but ensure it's present)
macro_rules! assert_success_with_json {
($result_jsonb:expr, $fmt:literal $(, $($args:tt)*)?) => {
let condition_result: Option<bool> = $result_jsonb.0.get("success").and_then(Value::as_bool);
@ -9,39 +10,138 @@ macro_rules! assert_success_with_json {
let base_msg = format!($fmt $(, $($args)*)?);
let pretty_json = serde_json::to_string_pretty(&$result_jsonb.0)
.unwrap_or_else(|_| format!("(Failed to pretty-print JSON: {:?})", $result_jsonb.0));
// Construct the full message string first
let panic_msg = format!("Assertion Failed (expected success): {}\nResult JSON:\n{}", base_msg, pretty_json);
panic!("{}", panic_msg); // Use the constructed string
panic!("{}", panic_msg);
}
};
// Simpler version without message
($result_jsonb:expr) => {
let condition_result: Option<bool> = $result_jsonb.0.get("success").and_then(Value::as_bool);
if condition_result != Some(true) {
let pretty_json = serde_json::to_string_pretty(&$result_jsonb.0)
.unwrap_or_else(|_| format!("(Failed to pretty-print JSON: {:?})", $result_jsonb.0));
let panic_msg = format!("Assertion Failed (expected success)\nResult JSON:\n{}", pretty_json);
panic!("{}", panic_msg);
}
};
}
// Helper macro for asserting failure with pretty JSON output on failure
// Updated helper macro for asserting failed JSON results with the new flat error structure
macro_rules! assert_failure_with_json {
($result_jsonb:expr, $fmt:literal $(, $($args:tt)*)?) => {
let condition_result: Option<bool> = $result_jsonb.0.get("success").and_then(Value::as_bool);
if condition_result != Some(false) {
// --- Arms with error count and message substring check ---
// With custom message:
($result:expr, $expected_error_count:expr, $expected_first_message_contains:expr, $fmt:literal $(, $($args:tt)*)?) => {
let json_result = &$result.0;
let success = json_result.get("success").and_then(Value::as_bool);
let errors_opt = json_result.get("errors").and_then(Value::as_array);
let base_msg = format!($fmt $(, $($args)*)?);
let pretty_json = serde_json::to_string_pretty(&$result_jsonb.0)
.unwrap_or_else(|_| format!("(Failed to pretty-print JSON: {:?})", $result_jsonb.0));
// Construct the full message string first
let panic_msg = format!("Assertion Failed (expected failure): {}\nResult JSON:\n{}", base_msg, pretty_json);
panic!("{}", panic_msg); // Use the constructed string
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): {}\nResult JSON:\n{}", base_msg, pretty_json);
}
match errors_opt {
Some(errors) => {
if errors.len() != $expected_error_count {
let pretty_json = serde_json::to_string_pretty(&json_result).unwrap_or_else(|_| format!("(Failed to pretty-print JSON: {:?})", json_result));
panic!("Assertion Failed (wrong error count): Expected {} errors, got {}. {}\nResult JSON:\n{}", $expected_error_count, errors.len(), base_msg, pretty_json);
}
if $expected_error_count > 0 {
let first_error_message = errors[0].get("message").and_then(Value::as_str);
match first_error_message {
Some(msg) => {
if !msg.contains($expected_first_message_contains) {
let pretty_json = serde_json::to_string_pretty(&json_result).unwrap_or_else(|_| format!("(Failed to pretty-print JSON: {:?})", json_result));
panic!("Assertion Failed (first error message mismatch): Expected contains '{}', got: '{}'. {}\nResult JSON:\n{}", $expected_first_message_contains, msg, base_msg, pretty_json);
}
}
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 (first error has no 'message' string): {}\nResult JSON:\n{}", base_msg, pretty_json);
}
}
}
}
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 'errors' array, but none found): {}\nResult JSON:\n{}", base_msg, pretty_json);
}
}
};
// Without custom message (calls the one above with ""):
($result:expr, $expected_error_count:expr, $expected_first_message_contains:expr) => {
assert_failure_with_json!($result, $expected_error_count, $expected_first_message_contains, "");
};
// --- Arms with error count check only ---
// With custom message:
($result:expr, $expected_error_count:expr, $fmt:literal $(, $($args:tt)*)?) => {
let json_result = &$result.0;
let success = json_result.get("success").and_then(Value::as_bool);
let errors_opt = json_result.get("errors").and_then(Value::as_array);
let base_msg = format!($fmt $(, $($args)*)?);
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): {}\nResult JSON:\n{}", base_msg, pretty_json);
}
match errors_opt {
Some(errors) => {
if errors.len() != $expected_error_count {
let pretty_json = serde_json::to_string_pretty(&json_result).unwrap_or_else(|_| format!("(Failed to pretty-print JSON: {:?})", json_result));
panic!("Assertion Failed (wrong error count): Expected {} errors, got {}. {}\nResult JSON:\n{}", $expected_error_count, errors.len(), base_msg, pretty_json);
}
}
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 'errors' array, but none found): {}\nResult JSON:\n{}", base_msg, pretty_json);
}
}
};
// Without custom message (calls the one above with ""):
($result:expr, $expected_error_count:expr) => {
assert_failure_with_json!($result, $expected_error_count, "");
};
// --- Arms checking failure only (expects at least one error) ---
// With custom message:
($result:expr, $fmt:literal $(, $($args:tt)*)?) => {
let json_result = &$result.0;
let success = json_result.get("success").and_then(Value::as_bool);
let errors_opt = json_result.get("errors").and_then(Value::as_array);
let base_msg = format!($fmt $(, $($args)*)?);
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): {}\nResult JSON:\n{}", base_msg, pretty_json);
}
match errors_opt {
Some(errors) => {
if errors.is_empty() {
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 errors, but errors array is empty): {}\nResult JSON:\n{}", base_msg, pretty_json);
}
}
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 'errors' array, but none found): {}\nResult JSON:\n{}", base_msg, pretty_json);
}
}
};
// Without custom message (calls the one above with ""):
($result:expr) => {
assert_failure_with_json!($result, "");
};
}
fn jsonb(val: Value) -> JsonB {
JsonB(val)
}
fn setup_test() {
clear_json_schemas();
}
#[pg_test]
fn test_cache_and_validate_json_schema() {
setup_test();
clear_json_schemas(); // Call clear directly
let schema_id = "my_schema";
let schema = json!({
"type": "object",
@ -61,61 +161,75 @@ fn test_cache_and_validate_json_schema() {
let valid_result = validate_json_schema(schema_id, jsonb(valid_instance));
assert_success_with_json!(valid_result, "Validation of valid instance should succeed.");
// Invalid type
let invalid_result_type = validate_json_schema(schema_id, jsonb(invalid_instance_type));
assert_failure_with_json!(invalid_result_type, "Validation with invalid type should fail.");
let error_obj_type = invalid_result_type.0.get("error").expect("Expected top-level 'error' object");
let causes_age = error_obj_type.get("error").and_then(Value::as_array).expect("Expected nested 'error' array (causes)");
assert!(!causes_age.is_empty(), "Expected causes for invalid age");
let age_error = causes_age.iter().find(|e| e["instance_path"].as_str() == Some("/age")).expect("Missing cause for /age instance path");
let age_error_message = age_error["message"].as_str().expect("Expected string message for age error");
assert_eq!(age_error_message, "must be >=0, but got -5", "Age error message mismatch: '{}'", age_error_message);
assert_failure_with_json!(invalid_result_type, 1, "must be >=0", "Validation with invalid type should fail.");
let errors_type = invalid_result_type.0["errors"].as_array().unwrap();
assert_eq!(errors_type[0]["instance_path"], "/age");
assert_eq!(errors_type[0]["schema_path"], "urn:my_schema#/properties/age");
// Missing field
let invalid_result_missing = validate_json_schema(schema_id, jsonb(invalid_instance_missing));
assert_failure_with_json!(invalid_result_missing, "Validation with missing field should fail.");
let error_obj_missing = invalid_result_missing.0.get("error").expect("Expected 'error' object for missing field");
let causes_missing = error_obj_missing.get("error").and_then(Value::as_array).expect("Expected nested 'error' array (causes)");
assert!(!causes_missing.is_empty(), "Expected causes for missing field");
let missing_prop_error = causes_missing.iter().find(|e| e["instance_path"].as_str() == Some("")).expect("Missing cause for root instance path");
let expected_missing_message = "missing properties 'age'";
assert_eq!(missing_prop_error["message"].as_str().expect("Expected string message for missing prop error"), expected_missing_message, "Missing property error message mismatch: '{}'", missing_prop_error["message"].as_str().unwrap_or("null"));
assert_failure_with_json!(invalid_result_missing, 1, "missing properties 'age'", "Validation with missing field should fail.");
let errors_missing = invalid_result_missing.0["errors"].as_array().unwrap();
assert_eq!(errors_missing[0]["instance_path"], "");
assert_eq!(errors_missing[0]["schema_path"], "urn:my_schema#");
// Schema not found
let non_existent_id = "non_existent_schema";
let invalid_schema_result = validate_json_schema(non_existent_id, jsonb(json!({})));
assert_failure_with_json!(invalid_schema_result, "Validation with non-existent schema should fail.");
let error_obj = invalid_schema_result.0.get("error").expect("Expected top-level 'error' object");
assert_eq!(error_obj["message"].as_str().expect("Expected message for cleared schema"), "Schema with id 'non_existent_schema' not found in cache");
assert_failure_with_json!(invalid_schema_result, 1, "Schema with id 'non_existent_schema' not found", "Validation with non-existent schema should fail.");
let errors_notfound = invalid_schema_result.0["errors"].as_array().unwrap();
assert_eq!(errors_notfound[0]["schema_path"], ""); // Schema path is empty for this error type
assert_eq!(errors_notfound[0]["instance_path"], ""); // Instance path is empty
}
#[pg_test]
fn test_validate_json_schema_not_cached() {
setup_test();
clear_json_schemas(); // Call clear directly
let instance = json!({ "foo": "bar" });
let result = validate_json_schema("non_existent_schema", jsonb(instance));
assert_failure_with_json!(result, "Validation with non-existent schema should fail.");
assert_eq!(result.0.get("success"), Some(&Value::Bool(false)), "Expected 'success': false for non-existent schema");
let error_obj = result.0.get("error").expect("Expected nested 'error' object for non-existent schema failure");
let error_message = error_obj["message"].as_str().expect("Expected message string for non-existent schema");
assert!(error_message.contains("non_existent_schema") && error_message.contains("not found"), "Error message mismatch for non-existent schema: '{}'", error_message);
// Use the updated macro
assert_failure_with_json!(result, 1, "Schema with id 'non_existent_schema' not found", "Validation with non-existent schema should fail.");
}
#[pg_test]
fn test_cache_invalid_json_schema() {
setup_test();
clear_json_schemas(); // Call clear directly
let schema_id = "invalid_schema";
let invalid_schema = json!({ "type": "invalid" });
let cache_result = cache_json_schema(schema_id, jsonb(invalid_schema));
assert_failure_with_json!(cache_result, "Caching invalid schema should fail.");
// Schema with an invalid type *value*
let invalid_schema = json!({
"$id": "urn:invalid_schema",
"type": ["invalid_type_value"]
});
let error_obj = cache_result.0.get("error").expect("Expected 'error' object for failed cache");
let message = error_obj.get("message").and_then(Value::as_str).expect("Message field missing");
let expected_message_start = &format!("Schema '{}' failed validation against its metaschema: ", schema_id);
assert!(message.starts_with(expected_message_start), "Compile error message start mismatch: {}", message);
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);
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);
}
}
#[pg_test]
fn test_validate_json_schema_detailed_validation_errors() {
setup_test();
clear_json_schemas(); // Call clear directly
let schema_id = "detailed_errors";
let schema = json!({
"type": "object",
@ -135,42 +249,32 @@ fn test_validate_json_schema_detailed_validation_errors() {
let invalid_instance = json!({
"address": {
"street": 123,
"city": "Supercalifragilisticexpialidocious"
"street": 123, // Wrong type
"city": "Supercalifragilisticexpialidocious" // Too long
}
});
let result = validate_json_schema(schema_id, jsonb(invalid_instance));
assert_failure_with_json!(result, "Validation should fail for detailed errors test.");
let error_obj = result.0.get("error").expect("Expected top-level 'error' object");
assert_eq!(error_obj["message"].as_str(), Some("Validation failed due to nested errors"));
// Update: Expect 2 errors again, as boon reports both nested errors.
assert_failure_with_json!(result, 2);
let causes_array = error_obj.get("error").and_then(Value::as_array).expect("Expected nested error array");
assert_eq!(causes_array.len(), 2, "Expected exactly 2 top-level errors (street type, city length)");
let street_error = causes_array.iter().find(|e| e["instance_path"].as_str() == Some("/address/street")).expect("Missing street error");
assert_eq!(street_error["message"].as_str().expect("Expected string message for street error"), "want string, but got number", "Street error message mismatch");
let city_error = causes_array.iter().find(|e| e["instance_path"].as_str() == Some("/address/city")).expect("Missing city error");
let expected_city_error_message = "length must be <=10, but got 34";
assert_eq!(city_error["message"].as_str().expect("Expected string message for city error"), expected_city_error_message, "City error message mismatch");
}
#[pg_test]
fn test_validate_json_schema_oneof_validation_errors() {
setup_test();
clear_json_schemas(); // Call clear directly
let schema_id = "oneof_schema";
let schema = json!({
"oneOf": [
{
{ // Option 1: Object with string prop
"type": "object",
"properties": {
"string_prop": { "type": "string", "maxLength": 5 }
},
"required": ["string_prop"]
},
{
{ // Option 2: Object with number prop
"type": "object",
"properties": {
"number_prop": { "type": "number", "minimum": 10 }
@ -182,52 +286,44 @@ fn test_validate_json_schema_oneof_validation_errors() {
let _ = cache_json_schema(schema_id, jsonb(schema));
let expected_urn = format!("urn:{}#", schema_id);
// --- Test case 1: String length failure ---
// --- Test case 1: Fails string maxLength (in branch 0) AND missing number_prop (in branch 1) ---
let invalid_string_instance = json!({ "string_prop": "toolongstring" });
let result_invalid_string = validate_json_schema(schema_id, jsonb(invalid_string_instance));
assert_failure_with_json!(result_invalid_string, "Validation with invalid string length should fail");
let error_obj_string = result_invalid_string.0.get("error").expect("Expected error object (string case)");
// Expect 2 leaf errors: one for maxLength (branch 0), one for missing prop (branch 1)
// Check the first error message reported by boon (maxLength).
assert_failure_with_json!(result_invalid_string, 2, "length must be <=5", "Validation with invalid string length should have 2 leaf errors");
let _errors_string = result_invalid_string.0["errors"].as_array().unwrap(); // Prefix with _
// Check the top-level error object directly: Input matches Branch 0 type, fails maxLength validation within it.
assert_eq!(error_obj_string["schema_path"].as_str(), Some(expected_urn.as_str()), "Schema path mismatch (string case)");
assert_eq!(error_obj_string["instance_path"].as_str(), Some(""), "Incorrect instance_path for string fail (string case)");
let actual_string_schema_fail_message = error_obj_string["message"].as_str().expect("Expected string message for string schema fail");
let expected_string_schema_fail_message = "Validation failed due to nested errors";
assert_eq!(actual_string_schema_fail_message, expected_string_schema_fail_message, "String schema fail message mismatch: '{}'", actual_string_schema_fail_message);
// --- Test case 2: Number type failure ---
// --- Test case 2: Fails number minimum (in branch 1) AND missing string_prop (in branch 0) ---
let invalid_number_instance = json!({ "number_prop": 5 });
let result_invalid_number = validate_json_schema(schema_id, jsonb(invalid_number_instance));
assert_failure_with_json!(result_invalid_number, "Validation with invalid number should fail");
let error_obj_number = result_invalid_number.0.get("error").expect("Expected error object (number case)");
// Expect 2 leaf errors: one for minimum (branch 1), one for missing prop (branch 0)
// Check the first error message reported by boon (missing prop).
assert_failure_with_json!(result_invalid_number, 2, "missing properties 'string_prop'", "Validation with invalid number should have 2 leaf errors");
let _errors_number = result_invalid_number.0["errors"].as_array().unwrap(); // Prefix with _
// Check the top-level error object directly: Input matches Branch 1 type, fails minimum validation within it.
assert_eq!(error_obj_number["schema_path"].as_str(), Some(expected_urn.as_str()), "Schema path mismatch (number case)");
assert_eq!(error_obj_number["instance_path"].as_str(), Some(""), "Incorrect instance_path for number fail (number case)");
let actual_number_schema_fail_message_num = error_obj_number["message"].as_str().expect("Expected string message for number schema fail (number case)");
let expected_number_schema_fail_message_num = "Validation failed due to nested errors";
assert_eq!(actual_number_schema_fail_message_num, expected_number_schema_fail_message_num, "Number schema fail message mismatch (number case): '{}'", actual_number_schema_fail_message_num);
// --- Test case 3: Bool type failure ---
let invalid_bool_instance = json!({ "bool_prop": 123 });
// --- Test case 3: Fails type check (not object) for both branches ---
// Input: boolean, expected object for both branches
let invalid_bool_instance = json!(true); // Not an object
let result_invalid_bool = validate_json_schema(schema_id, jsonb(invalid_bool_instance));
assert_failure_with_json!(result_invalid_bool, "Validation with invalid bool should fail");
let error_obj_bool = result_invalid_bool.0.get("error").expect("Expected error object (bool case)");
// Expect 2 leaf errors, one "Type" error for each branch
// Check the first error reported by boon (want object).
assert_failure_with_json!(result_invalid_bool, 2, "want object", "Validation with invalid bool should have 2 leaf errors");
let _errors_bool = result_invalid_bool.0["errors"].as_array().unwrap(); // Prefix with _
// Boon reports the failure for the first branch checked (Branch 0).
assert_eq!(error_obj_bool["schema_path"].as_str(), Some(expected_urn.as_str()), "Schema path mismatch (bool case)");
// Instance path is empty because the failure is at the root of the object being checked against Branch 0
assert_eq!(error_obj_bool["instance_path"].as_str(), Some(""), "Incorrect instance_path for bool fail");
let actual_bool_fail_message_0 = error_obj_bool["message"].as_str().expect("Expected string message for branch 0 type fail");
let expected_bool_fail_message_0 = "Validation failed due to nested errors";
assert_eq!(actual_bool_fail_message_0, expected_bool_fail_message_0, "Branch 0 type fail message mismatch: '{}'", actual_bool_fail_message_0);
// --- Test case 4: Fails missing required for both branches ---
// Input: empty object, expected string_prop (branch 0) OR number_prop (branch 1)
let invalid_empty_obj = json!({});
let result_empty_obj = validate_json_schema(schema_id, jsonb(invalid_empty_obj));
// Expect 2 leaf errors: one required error for branch 0, one required error for branch 1
// Check the first error reported by boon (missing string_prop).
assert_failure_with_json!(result_empty_obj, 2, "missing properties 'string_prop'", "Validation with empty object should have 2 leaf errors");
let _errors_empty = result_empty_obj.0["errors"].as_array().unwrap(); // Prefix with _
}
#[pg_test]
fn test_clear_json_schemas() {
setup_test();
clear_json_schemas(); // Call clear directly
let schema_id = "schema_to_clear";
let schema = json!({ "type": "string" });
cache_json_schema(schema_id, jsonb(schema.clone()));
@ -242,15 +338,13 @@ fn test_clear_json_schemas() {
let instance = json!("test");
let validate_result = validate_json_schema(schema_id, jsonb(instance));
assert_failure_with_json!(validate_result, "Validation should fail after clearing schemas.");
let error_obj = validate_result.0.get("error").expect("Expected top-level 'error' object");
assert_eq!(error_obj["message"].as_str().expect("Expected message for cleared schema"), "Schema with id 'schema_to_clear' not found in cache");
// Use the updated macro
assert_failure_with_json!(validate_result, 1, "Schema with id 'schema_to_clear' not found", "Validation should fail after clearing schemas.");
}
#[pg_test]
fn test_show_json_schemas() {
setup_test();
clear_json_schemas(); // Call clear directly
let schema_id1 = "schema1";
let schema_id2 = "schema2";
let schema = json!({ "type": "boolean" });
@ -258,7 +352,9 @@ fn test_show_json_schemas() {
cache_json_schema(schema_id1, jsonb(schema.clone()));
cache_json_schema(schema_id2, jsonb(schema.clone()));
let result = show_json_schemas();
assert!(result.contains(&schema_id1.to_string()));
let mut result = show_json_schemas(); // Make result mutable
result.sort(); // Sort for deterministic testing
assert_eq!(result, vec!["schema1".to_string(), "schema2".to_string()]); // Check exact content
assert!(result.contains(&schema_id1.to_string())); // Keep specific checks too if desired
assert!(result.contains(&schema_id2.to_string()));
}