fixed conditional errors with false schemas and unevaluatedProperties

This commit is contained in:
2025-06-12 22:27:49 -04:00
parent 1085964c17
commit cbda45e610
2 changed files with 197 additions and 11 deletions

View File

@ -91,25 +91,40 @@ fn cache_json_schema(schema_id: &str, schema: JsonB, strict: bool) -> JsonB {
}
}
// Helper function to recursively apply strict validation to all objects in a schema
// Helper function to apply strict validation to a schema
//
// This recursively adds unevaluatedProperties: false to object-type schemas,
// but SKIPS schemas inside if/then/else to avoid breaking conditional validation.
fn apply_strict_validation(schema: &mut Value) {
apply_strict_validation_recursive(schema, false);
}
fn apply_strict_validation_recursive(schema: &mut Value, inside_conditional: bool) {
match schema {
Value::Object(map) => {
// If this is an object type schema, add unevaluatedProperties: false
if let Some(Value::String(t)) = map.get("type") {
if t == "object" && !map.contains_key("unevaluatedProperties") && !map.contains_key("additionalProperties") {
map.insert("unevaluatedProperties".to_string(), Value::Bool(false));
// Skip adding strict validation if we're inside a conditional
if !inside_conditional {
// Add strict validation to object schemas only at top level
if let Some(Value::String(t)) = map.get("type") {
if t == "object" && !map.contains_key("unevaluatedProperties") && !map.contains_key("additionalProperties") {
// At top level, use unevaluatedProperties: false
// This considers all evaluated properties from all schemas
map.insert("unevaluatedProperties".to_string(), Value::Bool(false));
}
}
}
// Recurse into all properties
for (_, value) in map.iter_mut() {
apply_strict_validation(value);
for (key, value) in map.iter_mut() {
// Mark when we're inside conditional branches
let in_conditional = inside_conditional || matches!(key.as_str(), "if" | "then" | "else");
apply_strict_validation_recursive(value, in_conditional);
}
}
Value::Array(arr) => {
// Recurse into array items
// Recurse into array items
for item in arr.iter_mut() {
apply_strict_validation(item);
apply_strict_validation_recursive(item, inside_conditional);
}
}
_ => {}
@ -139,7 +154,9 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
let mut error_list = Vec::new();
collect_errors(&validation_error, &mut error_list);
let errors = format_errors(error_list, &instance_value, schema_id);
JsonB(json!({ "errors": errors }))
// Filter out FALSE_SCHEMA errors if there are other validation errors
let filtered_errors = filter_false_schema_errors(errors);
JsonB(json!({ "errors": filtered_errors }))
}
}
}
@ -154,6 +171,19 @@ fn collect_errors(error: &ValidationError, errors_list: &mut Vec<Error>) {
ErrorKind::Group | ErrorKind::AllOf | ErrorKind::AnyOf | ErrorKind::Not | ErrorKind::OneOf(_)
);
// Special handling for FalseSchema - if it has causes, use those instead
if matches!(&error.kind, ErrorKind::FalseSchema) {
if !error.causes.is_empty() {
// FalseSchema often wraps more specific errors in if/then conditionals
for cause in &error.causes {
collect_errors(cause, errors_list);
}
return;
}
// If FalseSchema has no causes, it's likely from unevaluatedProperties
// We'll handle it as a leaf error below
}
if error.causes.is_empty() && !is_structural {
// Handle errors with multiple fields specially
match &error.kind {
@ -223,6 +253,21 @@ fn collect_errors(error: &ValidationError, errors_list: &mut Vec<Error>) {
});
}
}
ErrorKind::Pattern { got, want } => {
// Special handling for pattern errors to include the pattern in the message
let display_value = if got.len() > 50 {
format!("{}...", &got[..50])
} else {
got.to_string()
};
errors_list.push(Error {
path: error.instance_location.to_string(),
code: "PATTERN_VIOLATED".to_string(),
message: format!("Value does not match pattern: {}", want),
cause: format!("'{}' does not match pattern {}", display_value, want),
});
}
_ => {
// Handle all other error types normally
let original_message = format!("{}", error.kind);
@ -403,6 +448,32 @@ fn convert_error_kind(kind: &ErrorKind) -> (String, String) {
}
}
// Filter out FALSE_SCHEMA errors if there are other validation errors
fn filter_false_schema_errors(errors: Vec<Value>) -> Vec<Value> {
// Check if there are any non-FALSE_SCHEMA errors
let has_non_false_schema = errors.iter().any(|e| {
e.get("code")
.and_then(|c| c.as_str())
.map(|code| code != "FALSE_SCHEMA")
.unwrap_or(false)
});
if has_non_false_schema {
// Filter out FALSE_SCHEMA errors
errors.into_iter()
.filter(|e| {
e.get("code")
.and_then(|c| c.as_str())
.map(|code| code != "FALSE_SCHEMA")
.unwrap_or(true)
})
.collect()
} else {
// Keep all errors (they're all FALSE_SCHEMA)
errors
}
}
// Formats errors according to DropError structure
fn format_errors(errors: Vec<Error>, instance: &Value, schema_id: &str) -> Vec<Value> {
// Deduplicate by instance_path and format as DropError