serializing ErrorKind directly to drop error cause

This commit is contained in:
2025-06-17 18:55:16 -04:00
parent 2dd17f0b37
commit 5fbf64bac5
2 changed files with 516 additions and 263 deletions

View File

@ -151,12 +151,17 @@ fn test_cache_and_validate_json_schema() {
// Invalid type - age is negative
let invalid_result_type = validate_json_schema(schema_id, jsonb(invalid_instance_type));
assert_failure_with_json!(invalid_result_type, 1, "Value is below the minimum allowed", "Validation with invalid type should fail.");
assert_failure_with_json!(invalid_result_type, 1, "Value must be at least 0, but got -5", "Validation with invalid type should fail.");
let errors_type = invalid_result_type.0["errors"].as_array().unwrap();
assert_eq!(errors_type[0]["details"]["path"], "/age");
assert_eq!(errors_type[0]["details"]["context"], -5);
assert_eq!(errors_type[0]["details"]["schema"], "my_schema");
assert_eq!(errors_type[0]["code"], "MINIMUM_VIOLATED");
// Check the cause is now a JSON object
let cause_type = &errors_type[0]["details"]["cause"];
assert!(cause_type.is_object());
assert_eq!(cause_type["got"], -5);
assert_eq!(cause_type["want"], 0);
// Missing field
let invalid_result_missing = validate_json_schema(schema_id, jsonb(invalid_instance_missing));
@ -165,6 +170,10 @@ fn test_cache_and_validate_json_schema() {
assert_eq!(errors_missing[0]["details"]["path"], "/age");
assert_eq!(errors_missing[0]["details"]["schema"], "my_schema");
assert_eq!(errors_missing[0]["code"], "REQUIRED_FIELD_MISSING");
// Check the cause is now a JSON object
let cause_missing = &errors_missing[0]["details"]["cause"];
assert!(cause_missing.is_object());
assert_eq!(cause_missing["want"], json!(["age"]));
// Schema not found
let non_existent_id = "non_existent_schema";
@ -173,6 +182,8 @@ fn test_cache_and_validate_json_schema() {
let errors_notfound = invalid_schema_result.0["errors"].as_array().unwrap();
assert_eq!(errors_notfound[0]["code"], "SCHEMA_NOT_FOUND");
assert_eq!(errors_notfound[0]["details"]["schema"], "non_existent_schema");
// Schema not found still has string cause (it's not from ErrorKind)
assert_eq!(errors_notfound[0]["details"]["cause"], "Schema must be cached before validation");
}
#[pg_test]
@ -200,7 +211,7 @@ fn test_cache_invalid_json_schema() {
assert_failure_with_json!(
cache_result,
2, // Expect exactly two leaf errors
"Value is not one of the allowed options", // Updated to human-readable message
"Value must be one of", // Updated to human-readable message
"Caching invalid schema should fail with specific meta-schema validation errors."
);
@ -219,6 +230,12 @@ fn test_cache_invalid_json_schema() {
// Check schema field is present
assert_eq!(errors_array[0]["details"]["schema"], "invalid_schema");
assert_eq!(errors_array[1]["details"]["schema"], "invalid_schema");
// Check that cause is now a JSON object with want array
for error in errors_array {
let cause = &error["details"]["cause"];
assert!(cause.is_object());
assert!(cause["want"].is_array());
}
}
#[pg_test]
@ -417,23 +434,32 @@ fn test_root_level_type_mismatch() {
// Test 1: Validate null against array schema (simulating delete_tokens issue)
let null_instance = json!(null);
let null_result = validate_json_schema(schema_id, jsonb(null_instance));
assert_failure_with_json!(null_result, 1, "Field type does not match the expected type");
assert_failure_with_json!(null_result, 1, "Expected array but got null");
let null_errors = null_result.0["errors"].as_array().unwrap();
assert_eq!(null_errors[0]["code"], "TYPE_MISMATCH");
assert_eq!(null_errors[0]["details"]["path"], ""); // Root level path should be empty string
assert_eq!(null_errors[0]["details"]["context"], json!(null));
assert_eq!(null_errors[0]["details"]["schema"], "array_schema");
assert!(null_errors[0]["details"]["cause"].as_str().unwrap().contains("want array"));
// Check cause is now a JSON object
let cause_null = &null_errors[0]["details"]["cause"];
assert!(cause_null.is_object());
assert_eq!(cause_null["got"], "null");
assert_eq!(cause_null["want"], json!(["array"]));
// Test 2: Validate object against array schema
let object_instance = json!({"id": "not-an-array"});
let object_result = validate_json_schema(schema_id, jsonb(object_instance.clone()));
assert_failure_with_json!(object_result, 1, "Field type does not match the expected type");
assert_failure_with_json!(object_result, 1, "Expected array but got object");
let object_errors = object_result.0["errors"].as_array().unwrap();
assert_eq!(object_errors[0]["code"], "TYPE_MISMATCH");
assert_eq!(object_errors[0]["details"]["path"], ""); // Root level path should be empty string
assert_eq!(object_errors[0]["details"]["context"], object_instance);
assert_eq!(object_errors[0]["details"]["schema"], "array_schema");
// Check cause is now a JSON object
let cause_object = &object_errors[0]["details"]["cause"];
assert!(cause_object.is_object());
assert_eq!(cause_object["got"], "object");
assert_eq!(cause_object["want"], json!(["array"]));
// Test 3: Valid empty array
let valid_empty = json!([]);
@ -454,12 +480,17 @@ fn test_root_level_type_mismatch() {
// String at root when object expected
let string_instance = json!("not an object");
let string_result = validate_json_schema(object_schema_id, jsonb(string_instance));
assert_failure_with_json!(string_result, 1, "Field type does not match the expected type");
assert_failure_with_json!(string_result, 1, "Expected object but got string");
let string_errors = string_result.0["errors"].as_array().unwrap();
assert_eq!(string_errors[0]["code"], "TYPE_MISMATCH");
assert_eq!(string_errors[0]["details"]["path"], ""); // Root level path
assert_eq!(string_errors[0]["details"]["schema"], "object_schema");
assert_eq!(string_errors[0]["details"]["context"], json!("not an object"));
// Check cause is now a JSON object
let cause_string = &string_errors[0]["details"]["cause"];
assert!(cause_string.is_object());
assert_eq!(cause_string["got"], "string");
assert_eq!(cause_string["want"], json!(["object"]));
}
#[pg_test]
@ -534,11 +565,15 @@ fn test_auto_strict_validation() {
// Should fail with strict schema
let result_root_strict = validate_json_schema(schema_id, jsonb(invalid_root_extra.clone()));
assert_failure_with_json!(result_root_strict, 1, "Schema validation always fails");
assert_failure_with_json!(result_root_strict, 1, "This schema always fails validation");
let errors_root = result_root_strict.0["errors"].as_array().unwrap();
assert_eq!(errors_root[0]["code"], "FALSE_SCHEMA");
assert_eq!(errors_root[0]["details"]["path"], "/extraField");
assert_eq!(errors_root[0]["details"]["schema"], "strict_test");
// Check cause is now a JSON object (empty for FalseSchema)
let cause_root = &errors_root[0]["details"]["cause"];
assert!(cause_root.is_object());
assert_eq!(cause_root, &json!({}));
// Should pass with non-strict schema
let result_root_non_strict = validate_json_schema(schema_id_non_strict, jsonb(invalid_root_extra));
@ -555,7 +590,7 @@ fn test_auto_strict_validation() {
// Should fail with strict schema
let result_nested_strict = validate_json_schema(schema_id, jsonb(invalid_nested_extra.clone()));
assert_failure_with_json!(result_nested_strict, 1, "Schema validation always fails");
assert_failure_with_json!(result_nested_strict, 1, "This schema always fails validation");
let errors_nested = result_nested_strict.0["errors"].as_array().unwrap();
assert_eq!(errors_nested[0]["code"], "FALSE_SCHEMA");
assert_eq!(errors_nested[0]["details"]["path"], "/profile/extraNested");
@ -579,7 +614,7 @@ fn test_auto_strict_validation() {
// Should fail with strict schema
let result_deep_strict = validate_json_schema(schema_id, jsonb(invalid_deep_extra.clone()));
assert_failure_with_json!(result_deep_strict, 1, "Schema validation always fails");
assert_failure_with_json!(result_deep_strict, 1, "This schema always fails validation");
let errors_deep = result_deep_strict.0["errors"].as_array().unwrap();
assert_eq!(errors_deep[0]["code"], "FALSE_SCHEMA");
assert_eq!(errors_deep[0]["details"]["path"], "/profile/preferences/extraDeep");
@ -599,7 +634,7 @@ fn test_auto_strict_validation() {
// Should fail with strict schema
let result_array_strict = validate_json_schema(schema_id, jsonb(invalid_array_item_extra.clone()));
assert_failure_with_json!(result_array_strict, 1, "Schema validation always fails");
assert_failure_with_json!(result_array_strict, 1, "This schema always fails validation");
let errors_array = result_array_strict.0["errors"].as_array().unwrap();
assert_eq!(errors_array[0]["code"], "FALSE_SCHEMA");
assert_eq!(errors_array[0]["details"]["path"], "/tags/0/extraInArray");
@ -680,7 +715,7 @@ fn test_auto_strict_validation() {
});
let result_invalid_conditional = validate_json_schema(schema_id_conditional, jsonb(invalid_conditional));
assert_failure_with_json!(result_invalid_conditional, 1, "Schema validation always fails");
assert_failure_with_json!(result_invalid_conditional, 1, "This schema always fails validation");
let errors_conditional = result_invalid_conditional.0["errors"].as_array().unwrap();
assert_eq!(errors_conditional[0]["code"], "FALSE_SCHEMA");
assert_eq!(errors_conditional[0]["details"]["path"], "/extra");
@ -1116,7 +1151,7 @@ fn test_unevaluated_properties_errors() {
let result = validate_json_schema(schema_id, jsonb(instance_uneval));
// Should get 3 separate FALSE_SCHEMA errors, one for each unevaluated property
assert_failure_with_json!(result, 3, "Schema validation always fails");
assert_failure_with_json!(result, 3, "This schema always fails validation");
let errors = result.0["errors"].as_array().unwrap();
@ -1177,7 +1212,7 @@ fn test_unevaluated_properties_errors() {
let complex_result = validate_json_schema(complex_schema_id, jsonb(complex_instance));
// Should get 2 FALSE_SCHEMA errors for unevaluated properties
assert_failure_with_json!(complex_result, 2, "Schema validation always fails");
assert_failure_with_json!(complex_result, 2, "This schema always fails validation");
let complex_errors = complex_result.0["errors"].as_array().unwrap();