Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d938058d34 | |||
| 69ab6165bb | |||
| 03beada825 | |||
| efdd7528cc |
20
src/lib.rs
20
src/lib.rs
@ -51,7 +51,7 @@ fn cache_json_schema(schema_id: &str, schema: JsonB, strict: bool) -> JsonB {
|
|||||||
"code": "SCHEMA_RESOURCE_ADD_FAILED",
|
"code": "SCHEMA_RESOURCE_ADD_FAILED",
|
||||||
"message": format!("Failed to add schema resource '{}'", schema_id),
|
"message": format!("Failed to add schema resource '{}'", schema_id),
|
||||||
"details": {
|
"details": {
|
||||||
"path": schema_path,
|
"schema": schema_id,
|
||||||
"cause": format!("{}", e)
|
"cause": format!("{}", e)
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
@ -72,7 +72,7 @@ fn cache_json_schema(schema_id: &str, schema: JsonB, strict: bool) -> JsonB {
|
|||||||
let mut error_list = Vec::new();
|
let mut error_list = Vec::new();
|
||||||
collect_errors(src, &mut error_list);
|
collect_errors(src, &mut error_list);
|
||||||
// Filter and format errors properly - no instance for schema compilation
|
// Filter and format errors properly - no instance for schema compilation
|
||||||
format_errors(error_list, &schema_value)
|
format_errors(error_list, &schema_value, schema_id)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
// Other compilation errors
|
// Other compilation errors
|
||||||
@ -80,7 +80,7 @@ fn cache_json_schema(schema_id: &str, schema: JsonB, strict: bool) -> JsonB {
|
|||||||
"code": "SCHEMA_COMPILATION_FAILED",
|
"code": "SCHEMA_COMPILATION_FAILED",
|
||||||
"message": format!("Schema '{}' compilation failed", schema_id),
|
"message": format!("Schema '{}' compilation failed", schema_id),
|
||||||
"details": {
|
"details": {
|
||||||
"path": schema_path,
|
"schema": schema_id,
|
||||||
"cause": format!("{:?}", e)
|
"cause": format!("{:?}", e)
|
||||||
}
|
}
|
||||||
})]
|
})]
|
||||||
@ -95,10 +95,10 @@ fn cache_json_schema(schema_id: &str, schema: JsonB, strict: bool) -> JsonB {
|
|||||||
fn apply_strict_validation(schema: &mut Value) {
|
fn apply_strict_validation(schema: &mut Value) {
|
||||||
match schema {
|
match schema {
|
||||||
Value::Object(map) => {
|
Value::Object(map) => {
|
||||||
// If this is an object type schema, add additionalProperties: false
|
// If this is an object type schema, add unevaluatedProperties: false
|
||||||
if let Some(Value::String(t)) = map.get("type") {
|
if let Some(Value::String(t)) = map.get("type") {
|
||||||
if t == "object" && !map.contains_key("additionalProperties") {
|
if t == "object" && !map.contains_key("unevaluatedProperties") && !map.contains_key("additionalProperties") {
|
||||||
map.insert("additionalProperties".to_string(), Value::Bool(false));
|
map.insert("unevaluatedProperties".to_string(), Value::Bool(false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Recurse into all properties
|
// Recurse into all properties
|
||||||
@ -126,6 +126,7 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
|||||||
"code": "SCHEMA_NOT_FOUND",
|
"code": "SCHEMA_NOT_FOUND",
|
||||||
"message": format!("Schema '{}' not found in cache", schema_id),
|
"message": format!("Schema '{}' not found in cache", schema_id),
|
||||||
"details": {
|
"details": {
|
||||||
|
"schema": schema_id,
|
||||||
"cause": "Schema must be cached before validation"
|
"cause": "Schema must be cached before validation"
|
||||||
}
|
}
|
||||||
}]
|
}]
|
||||||
@ -137,7 +138,7 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
|||||||
Err(validation_error) => {
|
Err(validation_error) => {
|
||||||
let mut error_list = Vec::new();
|
let mut error_list = Vec::new();
|
||||||
collect_errors(&validation_error, &mut error_list);
|
collect_errors(&validation_error, &mut error_list);
|
||||||
let errors = format_errors(error_list, &instance_value);
|
let errors = format_errors(error_list, &instance_value, schema_id);
|
||||||
JsonB(json!({ "errors": errors }))
|
JsonB(json!({ "errors": errors }))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -332,7 +333,7 @@ fn convert_error_kind(kind: &ErrorKind) -> (String, String) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Formats errors according to DropError structure
|
// Formats errors according to DropError structure
|
||||||
fn format_errors(errors: Vec<Error>, instance: &Value) -> Vec<Value> {
|
fn format_errors(errors: Vec<Error>, instance: &Value, schema_id: &str) -> Vec<Value> {
|
||||||
// Deduplicate by instance_path and format as DropError
|
// Deduplicate by instance_path and format as DropError
|
||||||
let mut unique_errors: HashMap<String, Value> = HashMap::new();
|
let mut unique_errors: HashMap<String, Value> = HashMap::new();
|
||||||
for error in errors {
|
for error in errors {
|
||||||
@ -345,7 +346,8 @@ fn format_errors(errors: Vec<Error>, instance: &Value) -> Vec<Value> {
|
|||||||
"details": {
|
"details": {
|
||||||
"path": error.path,
|
"path": error.path,
|
||||||
"context": failing_value,
|
"context": failing_value,
|
||||||
"cause": error.cause
|
"cause": error.cause,
|
||||||
|
"schema": schema_id
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
119
src/tests.rs
119
src/tests.rs
@ -155,6 +155,7 @@ fn test_cache_and_validate_json_schema() {
|
|||||||
let errors_type = invalid_result_type.0["errors"].as_array().unwrap();
|
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"]["path"], "/age");
|
||||||
assert_eq!(errors_type[0]["details"]["context"], -5);
|
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");
|
assert_eq!(errors_type[0]["code"], "MINIMUM_VIOLATED");
|
||||||
|
|
||||||
// Missing field
|
// Missing field
|
||||||
@ -162,6 +163,7 @@ fn test_cache_and_validate_json_schema() {
|
|||||||
assert_failure_with_json!(invalid_result_missing, 1, "Required field is missing", "Validation with missing field should fail.");
|
assert_failure_with_json!(invalid_result_missing, 1, "Required field is missing", "Validation with missing field should fail.");
|
||||||
let errors_missing = invalid_result_missing.0["errors"].as_array().unwrap();
|
let errors_missing = invalid_result_missing.0["errors"].as_array().unwrap();
|
||||||
assert_eq!(errors_missing[0]["details"]["path"], "");
|
assert_eq!(errors_missing[0]["details"]["path"], "");
|
||||||
|
assert_eq!(errors_missing[0]["details"]["schema"], "my_schema");
|
||||||
assert_eq!(errors_missing[0]["code"], "REQUIRED_FIELD_MISSING");
|
assert_eq!(errors_missing[0]["code"], "REQUIRED_FIELD_MISSING");
|
||||||
|
|
||||||
// Schema not found
|
// Schema not found
|
||||||
@ -170,6 +172,7 @@ fn test_cache_and_validate_json_schema() {
|
|||||||
assert_failure_with_json!(invalid_schema_result, 1, "Schema 'non_existent_schema' not found", "Validation with non-existent schema should fail.");
|
assert_failure_with_json!(invalid_schema_result, 1, "Schema 'non_existent_schema' not found", "Validation with non-existent schema should fail.");
|
||||||
let errors_notfound = invalid_schema_result.0["errors"].as_array().unwrap();
|
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]["code"], "SCHEMA_NOT_FOUND");
|
||||||
|
assert_eq!(errors_notfound[0]["details"]["schema"], "non_existent_schema");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pg_test]
|
#[pg_test]
|
||||||
@ -213,6 +216,9 @@ fn test_cache_invalid_json_schema() {
|
|||||||
.collect();
|
.collect();
|
||||||
assert!(paths.contains(&"/type"));
|
assert!(paths.contains(&"/type"));
|
||||||
assert!(paths.contains(&"/type/0"));
|
assert!(paths.contains(&"/type/0"));
|
||||||
|
// Check schema field is present
|
||||||
|
assert_eq!(errors_array[0]["details"]["schema"], "invalid_schema");
|
||||||
|
assert_eq!(errors_array[1]["details"]["schema"], "invalid_schema");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pg_test]
|
#[pg_test]
|
||||||
@ -283,11 +289,13 @@ fn test_validate_json_schema_oneof_validation_errors() {
|
|||||||
let errors_string = result_invalid_string.0["errors"].as_array().expect("Expected error array for invalid string");
|
let errors_string = result_invalid_string.0["errors"].as_array().expect("Expected error array for invalid string");
|
||||||
assert!(errors_string.iter().any(|e|
|
assert!(errors_string.iter().any(|e|
|
||||||
e["details"]["path"] == "/string_prop" &&
|
e["details"]["path"] == "/string_prop" &&
|
||||||
e["code"] == "MAX_LENGTH_VIOLATED"
|
e["code"] == "MAX_LENGTH_VIOLATED" &&
|
||||||
|
e["details"]["schema"] == "oneof_schema"
|
||||||
), "Missing maxLength error");
|
), "Missing maxLength error");
|
||||||
assert!(errors_string.iter().any(|e|
|
assert!(errors_string.iter().any(|e|
|
||||||
e["details"]["path"] == "" &&
|
e["details"]["path"] == "" &&
|
||||||
e["code"] == "REQUIRED_FIELD_MISSING"
|
e["code"] == "REQUIRED_FIELD_MISSING" &&
|
||||||
|
e["details"]["schema"] == "oneof_schema"
|
||||||
), "Missing number_prop required error");
|
), "Missing number_prop required error");
|
||||||
|
|
||||||
// --- Test case 2: Fails number minimum (in branch 1) AND missing string_prop (in branch 0) ---
|
// --- Test case 2: Fails number minimum (in branch 1) AND missing string_prop (in branch 0) ---
|
||||||
@ -299,11 +307,13 @@ fn test_validate_json_schema_oneof_validation_errors() {
|
|||||||
let errors_number = result_invalid_number.0["errors"].as_array().expect("Expected error array for invalid number");
|
let errors_number = result_invalid_number.0["errors"].as_array().expect("Expected error array for invalid number");
|
||||||
assert!(errors_number.iter().any(|e|
|
assert!(errors_number.iter().any(|e|
|
||||||
e["details"]["path"] == "/number_prop" &&
|
e["details"]["path"] == "/number_prop" &&
|
||||||
e["code"] == "MINIMUM_VIOLATED"
|
e["code"] == "MINIMUM_VIOLATED" &&
|
||||||
|
e["details"]["schema"] == "oneof_schema"
|
||||||
), "Missing minimum error");
|
), "Missing minimum error");
|
||||||
assert!(errors_number.iter().any(|e|
|
assert!(errors_number.iter().any(|e|
|
||||||
e["details"]["path"] == "" &&
|
e["details"]["path"] == "" &&
|
||||||
e["code"] == "REQUIRED_FIELD_MISSING"
|
e["code"] == "REQUIRED_FIELD_MISSING" &&
|
||||||
|
e["details"]["schema"] == "oneof_schema"
|
||||||
), "Missing string_prop required error");
|
), "Missing string_prop required error");
|
||||||
|
|
||||||
// --- Test case 3: Fails type check (not object) for both branches ---
|
// --- Test case 3: Fails type check (not object) for both branches ---
|
||||||
@ -317,6 +327,7 @@ fn test_validate_json_schema_oneof_validation_errors() {
|
|||||||
assert_eq!(errors_bool.len(), 1, "Expected exactly one error after deduplication");
|
assert_eq!(errors_bool.len(), 1, "Expected exactly one error after deduplication");
|
||||||
assert_eq!(errors_bool[0]["code"], "TYPE_MISMATCH");
|
assert_eq!(errors_bool[0]["code"], "TYPE_MISMATCH");
|
||||||
assert_eq!(errors_bool[0]["details"]["path"], "");
|
assert_eq!(errors_bool[0]["details"]["path"], "");
|
||||||
|
assert_eq!(errors_bool[0]["details"]["schema"], "oneof_schema");
|
||||||
|
|
||||||
// --- Test case 4: Fails missing required for both branches ---
|
// --- Test case 4: Fails missing required for both branches ---
|
||||||
// Input: empty object, expected string_prop (branch 0) OR number_prop (branch 1)
|
// Input: empty object, expected string_prop (branch 0) OR number_prop (branch 1)
|
||||||
@ -329,6 +340,7 @@ fn test_validate_json_schema_oneof_validation_errors() {
|
|||||||
assert_eq!(errors_empty.len(), 1, "Expected exactly one error after filtering empty object");
|
assert_eq!(errors_empty.len(), 1, "Expected exactly one error after filtering empty object");
|
||||||
assert_eq!(errors_empty[0]["code"], "REQUIRED_FIELD_MISSING");
|
assert_eq!(errors_empty[0]["code"], "REQUIRED_FIELD_MISSING");
|
||||||
assert_eq!(errors_empty[0]["details"]["path"], "");
|
assert_eq!(errors_empty[0]["details"]["path"], "");
|
||||||
|
assert_eq!(errors_empty[0]["details"]["schema"], "oneof_schema");
|
||||||
// The human message should be generic
|
// The human message should be generic
|
||||||
assert_eq!(errors_empty[0]["message"], "Required field is missing");
|
assert_eq!(errors_empty[0]["message"], "Required field is missing");
|
||||||
}
|
}
|
||||||
@ -376,6 +388,73 @@ fn test_show_json_schemas() {
|
|||||||
assert!(schemas.contains(&json!(schema_id2)));
|
assert!(schemas.contains(&json!(schema_id2)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[pg_test]
|
||||||
|
fn test_root_level_type_mismatch() {
|
||||||
|
clear_json_schemas();
|
||||||
|
let schema_id = "array_schema";
|
||||||
|
|
||||||
|
// Schema expecting an array (like delete_tokens response)
|
||||||
|
let schema = json!({
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"id": { "type": "string", "format": "uuid" }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let cache_result = cache_json_schema(schema_id, jsonb(schema), false);
|
||||||
|
assert_success_with_json!(cache_result, "Schema caching should succeed");
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
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"));
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
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");
|
||||||
|
|
||||||
|
// Test 3: Valid empty array
|
||||||
|
let valid_empty = json!([]);
|
||||||
|
let valid_result = validate_json_schema(schema_id, jsonb(valid_empty));
|
||||||
|
assert_success_with_json!(valid_result, "Empty array should be valid");
|
||||||
|
|
||||||
|
// Test 4: Schema expecting object at root
|
||||||
|
let object_schema_id = "object_schema";
|
||||||
|
let object_schema = json!({
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"name": { "type": "string" }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let _ = cache_json_schema(object_schema_id, jsonb(object_schema), false);
|
||||||
|
|
||||||
|
// 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");
|
||||||
|
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"));
|
||||||
|
}
|
||||||
|
|
||||||
#[pg_test]
|
#[pg_test]
|
||||||
fn test_auto_strict_validation() {
|
fn test_auto_strict_validation() {
|
||||||
clear_json_schemas();
|
clear_json_schemas();
|
||||||
@ -448,10 +527,11 @@ fn test_auto_strict_validation() {
|
|||||||
|
|
||||||
// Should fail with strict schema
|
// Should fail with strict schema
|
||||||
let result_root_strict = validate_json_schema(schema_id, jsonb(invalid_root_extra.clone()));
|
let result_root_strict = validate_json_schema(schema_id, jsonb(invalid_root_extra.clone()));
|
||||||
assert_failure_with_json!(result_root_strict, 1, "Object contains properties that are not allowed");
|
assert_failure_with_json!(result_root_strict, 1, "Schema validation always fails");
|
||||||
let errors_root = result_root_strict.0["errors"].as_array().unwrap();
|
let errors_root = result_root_strict.0["errors"].as_array().unwrap();
|
||||||
assert_eq!(errors_root[0]["code"], "ADDITIONAL_PROPERTIES_NOT_ALLOWED");
|
assert_eq!(errors_root[0]["code"], "FALSE_SCHEMA");
|
||||||
assert_eq!(errors_root[0]["details"]["path"], "");
|
assert_eq!(errors_root[0]["details"]["path"], "/extraField");
|
||||||
|
assert_eq!(errors_root[0]["details"]["schema"], "strict_test");
|
||||||
|
|
||||||
// Should pass with non-strict schema
|
// Should pass with non-strict schema
|
||||||
let result_root_non_strict = validate_json_schema(schema_id_non_strict, jsonb(invalid_root_extra));
|
let result_root_non_strict = validate_json_schema(schema_id_non_strict, jsonb(invalid_root_extra));
|
||||||
@ -468,10 +548,11 @@ fn test_auto_strict_validation() {
|
|||||||
|
|
||||||
// Should fail with strict schema
|
// Should fail with strict schema
|
||||||
let result_nested_strict = validate_json_schema(schema_id, jsonb(invalid_nested_extra.clone()));
|
let result_nested_strict = validate_json_schema(schema_id, jsonb(invalid_nested_extra.clone()));
|
||||||
assert_failure_with_json!(result_nested_strict, 1, "Object contains properties that are not allowed");
|
assert_failure_with_json!(result_nested_strict, 1, "Schema validation always fails");
|
||||||
let errors_nested = result_nested_strict.0["errors"].as_array().unwrap();
|
let errors_nested = result_nested_strict.0["errors"].as_array().unwrap();
|
||||||
assert_eq!(errors_nested[0]["code"], "ADDITIONAL_PROPERTIES_NOT_ALLOWED");
|
assert_eq!(errors_nested[0]["code"], "FALSE_SCHEMA");
|
||||||
assert_eq!(errors_nested[0]["details"]["path"], "/profile");
|
assert_eq!(errors_nested[0]["details"]["path"], "/profile/extraNested");
|
||||||
|
assert_eq!(errors_nested[0]["details"]["schema"], "strict_test");
|
||||||
|
|
||||||
// Should pass with non-strict schema
|
// Should pass with non-strict schema
|
||||||
let result_nested_non_strict = validate_json_schema(schema_id_non_strict, jsonb(invalid_nested_extra));
|
let result_nested_non_strict = validate_json_schema(schema_id_non_strict, jsonb(invalid_nested_extra));
|
||||||
@ -491,10 +572,11 @@ fn test_auto_strict_validation() {
|
|||||||
|
|
||||||
// Should fail with strict schema
|
// Should fail with strict schema
|
||||||
let result_deep_strict = validate_json_schema(schema_id, jsonb(invalid_deep_extra.clone()));
|
let result_deep_strict = validate_json_schema(schema_id, jsonb(invalid_deep_extra.clone()));
|
||||||
assert_failure_with_json!(result_deep_strict, 1, "Object contains properties that are not allowed");
|
assert_failure_with_json!(result_deep_strict, 1, "Schema validation always fails");
|
||||||
let errors_deep = result_deep_strict.0["errors"].as_array().unwrap();
|
let errors_deep = result_deep_strict.0["errors"].as_array().unwrap();
|
||||||
assert_eq!(errors_deep[0]["code"], "ADDITIONAL_PROPERTIES_NOT_ALLOWED");
|
assert_eq!(errors_deep[0]["code"], "FALSE_SCHEMA");
|
||||||
assert_eq!(errors_deep[0]["details"]["path"], "/profile/preferences");
|
assert_eq!(errors_deep[0]["details"]["path"], "/profile/preferences/extraDeep");
|
||||||
|
assert_eq!(errors_deep[0]["details"]["schema"], "strict_test");
|
||||||
|
|
||||||
// Should pass with non-strict schema
|
// Should pass with non-strict schema
|
||||||
let result_deep_non_strict = validate_json_schema(schema_id_non_strict, jsonb(invalid_deep_extra));
|
let result_deep_non_strict = validate_json_schema(schema_id_non_strict, jsonb(invalid_deep_extra));
|
||||||
@ -510,15 +592,16 @@ fn test_auto_strict_validation() {
|
|||||||
|
|
||||||
// Should fail with strict schema
|
// Should fail with strict schema
|
||||||
let result_array_strict = validate_json_schema(schema_id, jsonb(invalid_array_item_extra.clone()));
|
let result_array_strict = validate_json_schema(schema_id, jsonb(invalid_array_item_extra.clone()));
|
||||||
assert_failure_with_json!(result_array_strict, 1, "Object contains properties that are not allowed");
|
assert_failure_with_json!(result_array_strict, 1, "Schema validation always fails");
|
||||||
let errors_array = result_array_strict.0["errors"].as_array().unwrap();
|
let errors_array = result_array_strict.0["errors"].as_array().unwrap();
|
||||||
assert_eq!(errors_array[0]["code"], "ADDITIONAL_PROPERTIES_NOT_ALLOWED");
|
assert_eq!(errors_array[0]["code"], "FALSE_SCHEMA");
|
||||||
assert_eq!(errors_array[0]["details"]["path"], "/tags/0");
|
assert_eq!(errors_array[0]["details"]["path"], "/tags/0/extraInArray");
|
||||||
|
assert_eq!(errors_array[0]["details"]["schema"], "strict_test");
|
||||||
|
|
||||||
// Should pass with non-strict schema
|
// Should pass with non-strict schema
|
||||||
let result_array_non_strict = validate_json_schema(schema_id_non_strict, jsonb(invalid_array_item_extra));
|
let result_array_non_strict = validate_json_schema(schema_id_non_strict, jsonb(invalid_array_item_extra));
|
||||||
assert_success_with_json!(result_array_non_strict, "Extra array item property should be allowed with non-strict schema");
|
assert_success_with_json!(result_array_non_strict, "Extra array item property should be allowed with non-strict schema");
|
||||||
|
|
||||||
// Test 6: Schema with explicit additionalProperties: true should allow extras even with strict=true
|
// Test 6: Schema with explicit additionalProperties: true should allow extras even with strict=true
|
||||||
let schema_id_permissive = "permissive_test";
|
let schema_id_permissive = "permissive_test";
|
||||||
let permissive_schema = json!({
|
let permissive_schema = json!({
|
||||||
@ -538,4 +621,4 @@ fn test_auto_strict_validation() {
|
|||||||
|
|
||||||
let result_permissive = validate_json_schema(schema_id_permissive, jsonb(instance_with_extra));
|
let result_permissive = validate_json_schema(schema_id_permissive, jsonb(instance_with_extra));
|
||||||
assert_success_with_json!(result_permissive, "Instance with extra property should pass when additionalProperties is explicitly true, even with strict=true");
|
assert_success_with_json!(result_permissive, "Instance with extra property should pass when additionalProperties is explicitly true, even with strict=true");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user