From 3fdbf60396824d1998971dbc588ae0beeae6296e Mon Sep 17 00:00:00 2001 From: Alex Groleau Date: Fri, 12 Sep 2025 15:43:20 -0400 Subject: [PATCH] minor reorg no release --- src/lib.rs | 82 +++++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3e2c0b7..f067621 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -328,47 +328,6 @@ fn apply_strict_validation_recursive(schema: &mut Value, inside_conditional: boo } } -fn validate_type_against_schema_id(instance: &Value, schema_id: &str) -> JsonB { - let expected_type = schema_id.split('.').next().unwrap_or(schema_id); - - if let Some(actual_type) = instance.get("type").and_then(|v| v.as_str()) { - if actual_type == expected_type { - return JsonB(json!({ "response": "success" })); - } - } - - // If we reach here, validation failed. Now we build the specific error. - let (message, cause, context) = - if let Some(actual_type) = instance.get("type").and_then(|v| v.as_str()) { - // This handles the case where the type is a string but doesn't match. - ( - format!("Instance type '{}' does not match expected type '{}' derived from schema ID", actual_type, expected_type), - json!({ "expected": expected_type, "actual": actual_type }), - json!(actual_type) - ) - } else { - // This handles the case where 'type' is missing or not a string. - ( - "Instance 'type' property is missing or not a string".to_string(), - json!("The 'type' property must be a string and is required for this validation."), - instance.get("type").unwrap_or(&Value::Null).clone() - ) - }; - - JsonB(json!({ - "errors": [{ - "code": "TYPE_MISMATCH", - "message": message, - "details": { - "path": "/type", - "context": context, - "cause": cause, - "schema": schema_id - } - }] - })) -} - #[pg_extern(strict, parallel_safe)] fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB { let cache = SCHEMA_CACHE.read().unwrap(); @@ -414,6 +373,47 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB { } } +fn validate_type_against_schema_id(instance: &Value, schema_id: &str) -> JsonB { + // Get the main type (primary or before the first dot) and compare to type in instance + let expected_type = schema_id.split('.').next().unwrap_or(schema_id); + if let Some(actual_type) = instance.get("type").and_then(|v| v.as_str()) { + if actual_type == expected_type { + return JsonB(json!({ "response": "success" })); + } + } + + // If we reach here, validation failed. Now we build the specific error. + let (message, cause, context) = + if let Some(actual_type) = instance.get("type").and_then(|v| v.as_str()) { + // This handles the case where the type is a string but doesn't match. + ( + format!("Instance type '{}' does not match expected type '{}' derived from schema ID", actual_type, expected_type), + json!({ "expected": expected_type, "actual": actual_type }), + json!(actual_type) + ) + } else { + // This handles the case where 'type' is missing or not a string. + ( + "Instance 'type' property is missing or not a string".to_string(), + json!("The 'type' property must be a string and is required for this validation."), + instance.get("type").unwrap_or(&Value::Null).clone() + ) + }; + + JsonB(json!({ + "errors": [{ + "code": "TYPE_MISMATCH", + "message": message, + "details": { + "path": "/type", + "context": context, + "cause": cause, + "schema": schema_id + } + }] + })) +} + // Recursively collects validation errors fn collect_errors(error: &ValidationError, errors_list: &mut Vec) { // Check if this is a structural error that we should skip