minor reorg no release
This commit is contained in:
82
src/lib.rs
82
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)]
|
#[pg_extern(strict, parallel_safe)]
|
||||||
fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
||||||
let cache = SCHEMA_CACHE.read().unwrap();
|
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
|
// Recursively collects validation errors
|
||||||
fn collect_errors(error: &ValidationError, errors_list: &mut Vec<Error>) {
|
fn collect_errors(error: &ValidationError, errors_list: &mut Vec<Error>) {
|
||||||
// Check if this is a structural error that we should skip
|
// Check if this is a structural error that we should skip
|
||||||
|
|||||||
Reference in New Issue
Block a user