implemented type match checking for types on schema id instead of type const
This commit is contained in:
69
src/tests.rs
69
src/tests.rs
@ -506,6 +506,7 @@ fn test_validate_property_merging() {
|
||||
// entity (id, name) + user (password) + person (first_name, last_name)
|
||||
|
||||
let valid_person_with_all_properties = json!({
|
||||
"type": "person", // Added to satisfy new type check
|
||||
// From entity
|
||||
"id": "550e8400-e29b-41d4-a716-446655440000",
|
||||
"name": "John Doe",
|
||||
@ -523,6 +524,7 @@ fn test_validate_property_merging() {
|
||||
|
||||
// Test that properties validate according to their schema definitions across the chain
|
||||
let invalid_mixed_properties = json!({
|
||||
"type": "person", // Added to satisfy new type check
|
||||
"id": "550e8400-e29b-41d4-a716-446655440000",
|
||||
"name": "John Doe",
|
||||
"password": "short", // Too short from user schema
|
||||
@ -546,15 +548,13 @@ fn test_validate_required_merging() {
|
||||
// user: ["password"] (conditional when type=user)
|
||||
// person: ["first_name", "last_name"] (conditional when type=person)
|
||||
|
||||
let missing_all_required = json!({});
|
||||
let missing_all_required = json!({ "type": "person" }); // Add type to pass initial check
|
||||
|
||||
let result = validate_json_schema("person", jsonb(missing_all_required));
|
||||
// Should fail for all required fields across inheritance chain
|
||||
assert_error_count(&result, 6); // id, type, created_by, password, first_name, last_name
|
||||
// Should fail for all required fields across inheritance chain, except for the conditional 'password'
|
||||
assert_error_count(&result, 4); // id, created_by, first_name, last_name
|
||||
assert_has_error(&result, "REQUIRED_FIELD_MISSING", "/id");
|
||||
assert_has_error(&result, "REQUIRED_FIELD_MISSING", "/type");
|
||||
assert_has_error(&result, "REQUIRED_FIELD_MISSING", "/created_by");
|
||||
assert_has_error(&result, "REQUIRED_FIELD_MISSING", "/password");
|
||||
assert_has_error(&result, "REQUIRED_FIELD_MISSING", "/first_name");
|
||||
assert_has_error(&result, "REQUIRED_FIELD_MISSING", "/last_name");
|
||||
|
||||
@ -777,14 +777,69 @@ fn test_validate_title_override() {
|
||||
// Test that a schema with an overridden title still inherits validation keywords correctly.
|
||||
|
||||
// This instance is valid because it provides the 'name' required by the base schema.
|
||||
let valid_instance = json!({ "name": "Test Name" });
|
||||
let valid_instance = json!({ "type": "override_with_title", "name": "Test Name" });
|
||||
let result_valid = validate_json_schema("override_with_title", jsonb(valid_instance));
|
||||
assert_success(&result_valid);
|
||||
|
||||
// This instance is invalid because it's missing the 'name' required by the base schema.
|
||||
// This proves that validation keywords are inherited even when metadata keywords are overridden.
|
||||
let invalid_instance = json!({});
|
||||
let invalid_instance = json!({ "type": "override_with_title" });
|
||||
let result_invalid = validate_json_schema("override_with_title", jsonb(invalid_instance));
|
||||
assert_error_count(&result_invalid, 1);
|
||||
assert_has_error(&result_invalid, "REQUIRED_FIELD_MISSING", "/name");
|
||||
}
|
||||
|
||||
#[pg_test]
|
||||
fn test_validate_type_matching() {
|
||||
let cache_result = type_matching_schemas();
|
||||
assert_success(&cache_result);
|
||||
|
||||
// 1. Test 'job' which extends 'entity'
|
||||
let valid_job = json!({
|
||||
"type": "job",
|
||||
"name": "my job",
|
||||
"job_id": "job123"
|
||||
});
|
||||
let result_valid_job = validate_json_schema("job", jsonb(valid_job));
|
||||
assert_success(&result_valid_job);
|
||||
|
||||
let invalid_job = json!({
|
||||
"type": "not_job",
|
||||
"name": "my job",
|
||||
"job_id": "job123"
|
||||
});
|
||||
let result_invalid_job = validate_json_schema("job", jsonb(invalid_job));
|
||||
assert_error_count(&result_invalid_job, 1);
|
||||
assert_has_error(&result_invalid_job, "TYPE_MISMATCH", "/type");
|
||||
|
||||
// 2. Test 'super_job' which extends 'job'
|
||||
let valid_super_job = json!({
|
||||
"type": "super_job",
|
||||
"name": "my super job",
|
||||
"job_id": "job123",
|
||||
"manager_id": "mgr1"
|
||||
});
|
||||
let result_valid_super_job = validate_json_schema("super_job", jsonb(valid_super_job));
|
||||
assert_success(&result_valid_super_job);
|
||||
|
||||
// 3. Test 'super_job.short' which should still expect type 'super_job'
|
||||
let valid_short_super_job = json!({
|
||||
"type": "super_job",
|
||||
"name": "short", // maxLength: 10
|
||||
"job_id": "job123",
|
||||
"manager_id": "mgr1"
|
||||
});
|
||||
let result_valid_short = validate_json_schema("super_job.short", jsonb(valid_short_super_job));
|
||||
assert_success(&result_valid_short);
|
||||
|
||||
let invalid_short_super_job = json!({
|
||||
"type": "job", // Should be 'super_job'
|
||||
"name": "short",
|
||||
"job_id": "job123",
|
||||
"manager_id": "mgr1"
|
||||
});
|
||||
let result_invalid_short = validate_json_schema("super_job.short", jsonb(invalid_short_super_job));
|
||||
assert_error_count(&result_invalid_short, 1);
|
||||
let error = find_error_with_code_and_path(&result_invalid_short, "TYPE_MISMATCH", "/type");
|
||||
assert_error_message_contains(error, "Instance type 'job' does not match expected type 'super_job'");
|
||||
}
|
||||
Reference in New Issue
Block a user