Compare commits

...

19 Commits

Author SHA1 Message Date
dccaa0a46e version: 1.0.30 2025-07-04 04:23:15 -04:00
441597e604 need to allow empty strings when a string property has a format 2025-07-04 04:23:06 -04:00
710598752f version: 1.0.29 2025-06-17 18:55:27 -04:00
5fbf64bac5 serializing ErrorKind directly to drop error cause 2025-06-17 18:55:16 -04:00
2dd17f0b37 version: 1.0.28 2025-06-12 22:27:59 -04:00
cbda45e610 fixed conditional errors with false schemas and unevaluatedProperties 2025-06-12 22:27:49 -04:00
1085964c17 version: 1.0.27 2025-06-12 17:07:37 -04:00
65971d9b93 splitting up errorkind paths to produce multiple drop errors 2025-06-12 17:07:28 -04:00
d938058d34 version: 1.0.26 2025-06-12 00:59:44 -04:00
69ab6165bb improvements to error handling again 2025-06-12 00:59:33 -04:00
03beada825 version: 1.0.25 2025-06-11 20:28:46 -04:00
efdd7528cc switched strict validation from additionalProperties to unevaluatedProperties to catch conditional properties automatically in verification 2025-06-11 20:28:39 -04:00
59395a33ac version: 1.0.24 2025-06-11 19:38:56 -04:00
92c0a6fc0b even more jspg improved error handling, missing some codes before 2025-06-11 19:38:46 -04:00
7f66a4a35a no-op 2025-06-10 16:01:58 -04:00
d37aadb0dd version: 1.0.23 2025-06-09 18:09:33 -04:00
d0ccc47d97 added strict validation option 2025-06-09 18:09:15 -04:00
2d19bf100e version: 1.0.22 2025-06-06 14:25:18 -04:00
fb333c6cbb slight improvements to error messaging 2025-06-06 14:25:13 -04:00
4 changed files with 1612 additions and 251 deletions

1
rustfmt.toml Normal file
View File

@ -0,0 +1 @@
tab_spaces = 2

View File

@ -2,16 +2,27 @@ use pgrx::*;
pg_module_magic!();
use serde_json::{json, Value};
use std::{collections::HashMap, sync::RwLock};
use boon::{Compiler, Schemas, ValidationError, SchemaIndex, CompileError};
use boon::{CompileError, Compiler, ErrorKind, SchemaIndex, Schemas, ValidationError, Type, Types};
use lazy_static::lazy_static;
use serde_json::{json, Value, Number};
use std::borrow::Cow;
use std::collections::hash_map::Entry;
use std::{collections::HashMap, sync::RwLock};
struct BoonCache {
schemas: Schemas,
id_to_index: HashMap<String, SchemaIndex>,
}
// Structure to hold error information without lifetimes
#[derive(Debug)]
struct Error {
path: String,
code: String,
message: String,
cause: Value, // Changed from String to Value to store JSON
}
lazy_static! {
static ref SCHEMA_CACHE: RwLock<BoonCache> = RwLock::new(BoonCache {
schemas: Schemas::new(),
@ -20,11 +31,17 @@ lazy_static! {
}
#[pg_extern(strict)]
fn cache_json_schema(schema_id: &str, schema: JsonB) -> JsonB {
fn cache_json_schema(schema_id: &str, schema: JsonB, strict: bool) -> JsonB {
let mut cache = SCHEMA_CACHE.write().unwrap();
let schema_value: Value = schema.0;
let mut schema_value: Value = schema.0;
let schema_path = format!("urn:{}", schema_id);
// Apply strict validation to all objects in the schema if requested
if strict {
apply_strict_validation(&mut schema_value);
}
// Create the boon compiler and enable format assertions
let mut compiler = Compiler::new();
compiler.enable_format_assertions();
@ -35,7 +52,7 @@ fn cache_json_schema(schema_id: &str, schema: JsonB) -> JsonB {
"code": "SCHEMA_RESOURCE_ADD_FAILED",
"message": format!("Failed to add schema resource '{}'", schema_id),
"details": {
"path": schema_path,
"schema": schema_id,
"cause": format!("{}", e)
}
}]
@ -54,9 +71,9 @@ fn cache_json_schema(schema_id: &str, schema: JsonB) -> JsonB {
CompileError::ValidationError { url: _url, src } => {
// Collect leaf errors from the meta-schema validation failure
let mut error_list = Vec::new();
collect_validation_errors(src, &mut error_list);
collect_errors(src, &mut error_list);
// Filter and format errors properly - no instance for schema compilation
format_drop_errors(error_list, &schema_value)
format_errors(error_list, &schema_value, schema_id)
}
_ => {
// Other compilation errors
@ -64,7 +81,7 @@ fn cache_json_schema(schema_id: &str, schema: JsonB) -> JsonB {
"code": "SCHEMA_COMPILATION_FAILED",
"message": format!("Schema '{}' compilation failed", schema_id),
"details": {
"path": schema_path,
"schema": schema_id,
"cause": format!("{:?}", e)
}
})]
@ -75,10 +92,49 @@ fn cache_json_schema(schema_id: &str, schema: JsonB) -> JsonB {
}
}
// 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) => {
// 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 (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
for item in arr.iter_mut() {
apply_strict_validation_recursive(item, inside_conditional);
}
}
_ => {}
}
}
#[pg_extern(strict, parallel_safe)]
fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
let cache = SCHEMA_CACHE.read().unwrap();
// Lookup uses the original schema_id
match cache.id_to_index.get(schema_id) {
None => JsonB(json!({
@ -86,6 +142,7 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
"code": "SCHEMA_NOT_FOUND",
"message": format!("Schema '{}' not found in cache", schema_id),
"details": {
"schema": schema_id,
"cause": "Schema must be cached before validation"
}
}]
@ -96,10 +153,15 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
Ok(_) => JsonB(json!({ "response": "success" })),
Err(validation_error) => {
let mut error_list = Vec::new();
collect_validation_errors(&validation_error, &mut error_list);
let errors = format_drop_errors(error_list, &instance_value);
JsonB(json!({ "errors": errors }))
collect_errors(&validation_error, &mut error_list);
let errors = format_errors(error_list, &instance_value, schema_id);
// Filter out FALSE_SCHEMA errors if there are other validation errors
let filtered_errors = filter_false_schema_errors(errors);
if filtered_errors.is_empty() {
JsonB(json!({ "response": "success" }))
} else {
JsonB(json!({ "errors": filtered_errors }))
}
}
}
}
@ -107,206 +169,594 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
}
// Recursively collects validation errors
fn collect_validation_errors(error: &ValidationError, errors_list: &mut Vec<(String, String, String)>) {
fn collect_errors(error: &ValidationError, errors_list: &mut Vec<Error>) {
// Check if this is a structural error that we should skip
let error_message = format!("{}", error.kind);
let is_structural = error_message == "validation failed" ||
error_message == "allOf failed" ||
error_message == "anyOf failed" ||
error_message == "not failed" ||
error_message.starts_with("oneOf failed");
if error.causes.is_empty() && !is_structural {
// This is a leaf error that's not structural
// Format just the error kind, not the whole validation error
let message = format!("{}", error.kind);
let is_structural = matches!(
&error.kind,
ErrorKind::Group | ErrorKind::AllOf | ErrorKind::AnyOf | ErrorKind::Not | ErrorKind::OneOf(_)
);
errors_list.push((
error.instance_location.to_string(),
error.schema_url.to_string(),
message
));
// 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 {
let base_path = error.instance_location.to_string();
// Match on error kind and handle each type
let errors_to_add = match &error.kind {
ErrorKind::Type { got, want } => handle_type_error(&base_path, got, want),
ErrorKind::Required { want } => handle_required_error(&base_path, want),
ErrorKind::Dependency { prop, missing } => handle_dependency_error(&base_path, prop, missing, false),
ErrorKind::DependentRequired { prop, missing } => handle_dependency_error(&base_path, prop, missing, true),
ErrorKind::AdditionalProperties { got } => handle_additional_properties_error(&base_path, got),
ErrorKind::Enum { want } => handle_enum_error(&base_path, want),
ErrorKind::Const { want } => handle_const_error(&base_path, want),
ErrorKind::MinLength { got, want } => handle_min_length_error(&base_path, *got, *want),
ErrorKind::MaxLength { got, want } => handle_max_length_error(&base_path, *got, *want),
ErrorKind::Pattern { got, want } => handle_pattern_error(&base_path, got, want),
ErrorKind::Minimum { got, want } => handle_minimum_error(&base_path, got, want),
ErrorKind::Maximum { got, want } => handle_maximum_error(&base_path, got, want),
ErrorKind::ExclusiveMinimum { got, want } => handle_exclusive_minimum_error(&base_path, got, want),
ErrorKind::ExclusiveMaximum { got, want } => handle_exclusive_maximum_error(&base_path, got, want),
ErrorKind::MultipleOf { got, want } => handle_multiple_of_error(&base_path, got, want),
ErrorKind::MinItems { got, want } => handle_min_items_error(&base_path, *got, *want),
ErrorKind::MaxItems { got, want } => handle_max_items_error(&base_path, *got, *want),
ErrorKind::UniqueItems { got } => handle_unique_items_error(&base_path, got),
ErrorKind::MinProperties { got, want } => handle_min_properties_error(&base_path, *got, *want),
ErrorKind::MaxProperties { got, want } => handle_max_properties_error(&base_path, *got, *want),
ErrorKind::AdditionalItems { got } => handle_additional_items_error(&base_path, *got),
ErrorKind::Format { want, got, err } => handle_format_error(&base_path, want, got, err),
ErrorKind::PropertyName { prop } => handle_property_name_error(&base_path, prop),
ErrorKind::Contains => handle_contains_error(&base_path),
ErrorKind::MinContains { got, want } => handle_min_contains_error(&base_path, got, *want),
ErrorKind::MaxContains { got, want } => handle_max_contains_error(&base_path, got, *want),
ErrorKind::ContentEncoding { want, err } => handle_content_encoding_error(&base_path, want, err),
ErrorKind::ContentMediaType { want, err, .. } => handle_content_media_type_error(&base_path, want, err),
ErrorKind::FalseSchema => handle_false_schema_error(&base_path),
ErrorKind::Not => handle_not_error(&base_path),
ErrorKind::RefCycle { url, kw_loc1, kw_loc2 } => handle_ref_cycle_error(&base_path, url, kw_loc1, kw_loc2),
ErrorKind::Reference { kw, url } => handle_reference_error(&base_path, kw, url),
ErrorKind::Schema { url } => handle_schema_error(&base_path, url),
ErrorKind::ContentSchema => handle_content_schema_error(&base_path),
ErrorKind::Group => handle_group_error(&base_path),
ErrorKind::AllOf => handle_all_of_error(&base_path),
ErrorKind::AnyOf => handle_any_of_error(&base_path),
ErrorKind::OneOf(matched) => handle_one_of_error(&base_path, matched),
};
// Add all generated errors
for error in errors_to_add {
errors_list.push(error);
}
} else {
// Recurse into causes
for cause in &error.causes {
collect_validation_errors(cause, errors_list);
collect_errors(cause, errors_list);
}
}
}
// Formats errors according to DropError structure
fn format_drop_errors(raw_errors: Vec<(String, String, String)>, instance: &Value) -> Vec<Value> {
use std::collections::HashMap;
use std::collections::hash_map::Entry;
// Handler functions for each error kind
fn handle_type_error(base_path: &str, got: &Type, want: &Types) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "TYPE_MISMATCH".to_string(),
message: format!("Expected {} but got {}",
want.iter().map(|t| t.to_string()).collect::<Vec<_>>().join(" or "),
got
),
cause: json!({
"got": got.to_string(),
"want": want.iter().map(|t| t.to_string()).collect::<Vec<_>>()
}),
}]
}
// We don't filter structural paths from instance paths anymore
// because instance paths shouldn't contain these segments anyway
// The issue was likely with schema paths, not instance paths
let plausible_errors = raw_errors;
// 2. Deduplicate by instance_path and format as DropError
let mut unique_errors: HashMap<String, Value> = HashMap::new();
for (instance_path, schema_path, message) in plausible_errors {
if let Entry::Vacant(entry) = unique_errors.entry(instance_path.clone()) {
// Convert message to error code and make it human readable
let (code, human_message) = enhance_error_message(&message);
// Extract the failing value from the instance
let failing_value = extract_value_at_path(instance, &instance_path);
entry.insert(json!({
"code": code,
"message": human_message,
"details": {
"path": schema_path,
"context": json!({
"instance_path": instance_path,
"failing_value": failing_value
}),
"cause": message // Original error message
}
}));
}
fn handle_required_error(base_path: &str, want: &[&str]) -> Vec<Error> {
// Create a separate error for each missing required field
want.iter().map(|missing_field| {
let field_path = if base_path.is_empty() {
format!("/{}", missing_field)
} else {
format!("{}/{}", base_path, missing_field)
};
Error {
path: field_path,
code: "REQUIRED_FIELD_MISSING".to_string(),
message: format!("Required field '{}' is missing", missing_field),
cause: json!({ "want": [missing_field] }),
}
}).collect()
}
unique_errors.into_values().collect()
fn handle_dependency_error(base_path: &str, prop: &str, missing: &[&str], is_dependent_required: bool) -> Vec<Error> {
// Create a separate error for each missing field
missing.iter().map(|missing_field| {
let field_path = if base_path.is_empty() {
format!("/{}", missing_field)
} else {
format!("{}/{}", base_path, missing_field)
};
let (code, message) = if is_dependent_required {
(
"DEPENDENT_REQUIRED_MISSING".to_string(),
format!("Field '{}' is required when '{}' is present", missing_field, prop),
)
} else {
(
"DEPENDENCY_FAILED".to_string(),
format!("Field '{}' is required when '{}' is present", missing_field, prop),
)
};
Error {
path: field_path,
code,
message,
cause: json!({ "prop": prop, "missing": [missing_field] }),
}
}).collect()
}
fn handle_additional_properties_error(base_path: &str, got: &[Cow<str>]) -> Vec<Error> {
// Create a separate error for each additional property that's not allowed
got.iter().map(|extra_prop| {
let field_path = if base_path.is_empty() {
format!("/{}", extra_prop)
} else {
format!("{}/{}", base_path, extra_prop)
};
Error {
path: field_path,
code: "ADDITIONAL_PROPERTIES_NOT_ALLOWED".to_string(),
message: format!("Property '{}' is not allowed", extra_prop),
cause: json!({ "got": [extra_prop.to_string()] }),
}
}).collect()
}
fn handle_enum_error(base_path: &str, want: &[Value]) -> Vec<Error> {
let message = if want.len() == 1 {
format!("Value must be {}", serde_json::to_string(&want[0]).unwrap_or_else(|_| "unknown".to_string()))
} else {
format!("Value must be one of: {}",
want.iter()
.map(|v| serde_json::to_string(v).unwrap_or_else(|_| "unknown".to_string()))
.collect::<Vec<_>>()
.join(", ")
)
};
vec![Error {
path: base_path.to_string(),
code: "ENUM_VIOLATED".to_string(),
message,
cause: json!({ "want": want }),
}]
}
fn handle_const_error(base_path: &str, want: &Value) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "CONST_VIOLATED".to_string(),
message: format!("Value must be exactly {}", serde_json::to_string(want).unwrap_or_else(|_| "unknown".to_string())),
cause: json!({ "want": want }),
}]
}
fn handle_min_length_error(base_path: &str, got: usize, want: usize) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "MIN_LENGTH_VIOLATED".to_string(),
message: format!("String length must be at least {} characters, but got {}", want, got),
cause: json!({ "got": got, "want": want }),
}]
}
fn handle_max_length_error(base_path: &str, got: usize, want: usize) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "MAX_LENGTH_VIOLATED".to_string(),
message: format!("String length must be at most {} characters, but got {}", want, got),
cause: json!({ "got": got, "want": want }),
}]
}
fn handle_pattern_error(base_path: &str, got: &Cow<str>, want: &str) -> Vec<Error> {
let display_value = if got.len() > 50 {
format!("{}...", &got[..50])
} else {
got.to_string()
};
vec![Error {
path: base_path.to_string(),
code: "PATTERN_VIOLATED".to_string(),
message: format!("Value '{}' does not match pattern '{}'", display_value, want),
cause: json!({ "got": got.to_string(), "want": want }),
}]
}
fn handle_minimum_error(base_path: &str, got: &Cow<Number>, want: &Number) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "MINIMUM_VIOLATED".to_string(),
message: format!("Value must be at least {}, but got {}", want, got),
cause: json!({ "got": got, "want": want }),
}]
}
fn handle_maximum_error(base_path: &str, got: &Cow<Number>, want: &Number) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "MAXIMUM_VIOLATED".to_string(),
message: format!("Value must be at most {}, but got {}", want, got),
cause: json!({ "got": got, "want": want }),
}]
}
fn handle_exclusive_minimum_error(base_path: &str, got: &Cow<Number>, want: &Number) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "EXCLUSIVE_MINIMUM_VIOLATED".to_string(),
message: format!("Value must be greater than {}, but got {}", want, got),
cause: json!({ "got": got, "want": want }),
}]
}
fn handle_exclusive_maximum_error(base_path: &str, got: &Cow<Number>, want: &Number) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "EXCLUSIVE_MAXIMUM_VIOLATED".to_string(),
message: format!("Value must be less than {}, but got {}", want, got),
cause: json!({ "got": got, "want": want }),
}]
}
fn handle_multiple_of_error(base_path: &str, got: &Cow<Number>, want: &Number) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "MULTIPLE_OF_VIOLATED".to_string(),
message: format!("{} is not a multiple of {}", got, want),
cause: json!({ "got": got, "want": want }),
}]
}
fn handle_min_items_error(base_path: &str, got: usize, want: usize) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "MIN_ITEMS_VIOLATED".to_string(),
message: format!("Array must have at least {} items, but has {}", want, got),
cause: json!({ "got": got, "want": want }),
}]
}
fn handle_max_items_error(base_path: &str, got: usize, want: usize) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "MAX_ITEMS_VIOLATED".to_string(),
message: format!("Array must have at most {} items, but has {}", want, got),
cause: json!({ "got": got, "want": want }),
}]
}
fn handle_unique_items_error(base_path: &str, got: &[usize; 2]) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "UNIQUE_ITEMS_VIOLATED".to_string(),
message: format!("Array items at positions {} and {} are duplicates", got[0], got[1]),
cause: json!({ "got": got }),
}]
}
fn handle_min_properties_error(base_path: &str, got: usize, want: usize) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "MIN_PROPERTIES_VIOLATED".to_string(),
message: format!("Object must have at least {} properties, but has {}", want, got),
cause: json!({ "got": got, "want": want }),
}]
}
fn handle_max_properties_error(base_path: &str, got: usize, want: usize) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "MAX_PROPERTIES_VIOLATED".to_string(),
message: format!("Object must have at most {} properties, but has {}", want, got),
cause: json!({ "got": got, "want": want }),
}]
}
fn handle_additional_items_error(base_path: &str, got: usize) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "ADDITIONAL_ITEMS_NOT_ALLOWED".to_string(),
message: format!("Last {} array items are not allowed", got),
cause: json!({ "got": got }),
}]
}
fn handle_format_error(base_path: &str, want: &str, got: &Cow<Value>, err: &Box<dyn std::error::Error>) -> Vec<Error> {
// If the value is an empty string, skip format validation.
if let Value::String(s) = got.as_ref() {
if s.is_empty() {
return vec![];
}
}
vec![Error {
path: base_path.to_string(),
code: "FORMAT_INVALID".to_string(),
message: format!("Value {} is not a valid {} format",
serde_json::to_string(got.as_ref()).unwrap_or_else(|_| "unknown".to_string()),
want
),
cause: json!({ "got": got, "want": want, "err": err.to_string() }),
}]
}
fn handle_property_name_error(base_path: &str, prop: &str) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "INVALID_PROPERTY_NAME".to_string(),
message: format!("Property name '{}' is invalid", prop),
cause: json!({ "prop": prop }),
}]
}
fn handle_contains_error(base_path: &str) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "CONTAINS_FAILED".to_string(),
message: "No array items match the required schema".to_string(),
cause: json!({}),
}]
}
fn handle_min_contains_error(base_path: &str, got: &[usize], want: usize) -> Vec<Error> {
let message = if got.is_empty() {
format!("At least {} array items must match the schema, but none do", want)
} else {
format!("At least {} array items must match the schema, but only {} do (at positions {})",
want,
got.len(),
got.iter().map(|i| i.to_string()).collect::<Vec<_>>().join(", ")
)
};
vec![Error {
path: base_path.to_string(),
code: "MIN_CONTAINS_VIOLATED".to_string(),
message,
cause: json!({ "got": got, "want": want }),
}]
}
fn handle_max_contains_error(base_path: &str, got: &[usize], want: usize) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "MAX_CONTAINS_VIOLATED".to_string(),
message: format!("At most {} array items can match the schema, but {} do (at positions {})",
want,
got.len(),
got.iter().map(|i| i.to_string()).collect::<Vec<_>>().join(", ")
),
cause: json!({ "got": got, "want": want }),
}]
}
fn handle_content_encoding_error(base_path: &str, want: &str, err: &Box<dyn std::error::Error>) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "CONTENT_ENCODING_INVALID".to_string(),
message: format!("Content is not valid {} encoding: {}", want, err),
cause: json!({ "want": want, "err": err.to_string() }),
}]
}
fn handle_content_media_type_error(base_path: &str, want: &str, err: &Box<dyn std::error::Error>) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "CONTENT_MEDIA_TYPE_INVALID".to_string(),
message: format!("Content is not valid {} media type: {}", want, err),
cause: json!({ "want": want, "err": err.to_string() }),
}]
}
fn handle_false_schema_error(base_path: &str) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "FALSE_SCHEMA".to_string(),
message: "This schema always fails validation".to_string(),
cause: json!({}),
}]
}
fn handle_not_error(base_path: &str) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "NOT_VIOLATED".to_string(),
message: "Value matches a schema that it should not match".to_string(),
cause: json!({}),
}]
}
fn handle_ref_cycle_error(base_path: &str, url: &str, kw_loc1: &str, kw_loc2: &str) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "REFERENCE_CYCLE".to_string(),
message: format!("Reference cycle detected: both '{}' and '{}' resolve to '{}'", kw_loc1, kw_loc2, url),
cause: json!({ "url": url, "kw_loc1": kw_loc1, "kw_loc2": kw_loc2 }),
}]
}
fn handle_reference_error(base_path: &str, kw: &str, url: &str) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "REFERENCE_FAILED".to_string(),
message: format!("{} reference to '{}' failed validation", kw, url),
cause: json!({ "kw": kw, "url": url }),
}]
}
fn handle_schema_error(base_path: &str, url: &str) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "SCHEMA_FAILED".to_string(),
message: format!("Schema '{}' validation failed", url),
cause: json!({ "url": url }),
}]
}
fn handle_content_schema_error(base_path: &str) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "CONTENT_SCHEMA_FAILED".to_string(),
message: "Content schema validation failed".to_string(),
cause: json!({}),
}]
}
fn handle_group_error(base_path: &str) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "VALIDATION_FAILED".to_string(),
message: "Validation failed".to_string(),
cause: json!({}),
}]
}
fn handle_all_of_error(base_path: &str) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "ALL_OF_VIOLATED".to_string(),
message: "Value does not match all required schemas".to_string(),
cause: json!({}),
}]
}
fn handle_any_of_error(base_path: &str) -> Vec<Error> {
vec![Error {
path: base_path.to_string(),
code: "ANY_OF_VIOLATED".to_string(),
message: "Value does not match any of the allowed schemas".to_string(),
cause: json!({}),
}]
}
fn handle_one_of_error(base_path: &str, matched: &Option<(usize, usize)>) -> Vec<Error> {
let (message, cause) = match matched {
None => (
"Value must match exactly one schema, but matches none".to_string(),
json!({ "matched_indices": null })
),
Some((i, j)) => (
format!("Value must match exactly one schema, but matches schemas at positions {} and {}", i, j),
json!({ "matched_indices": [i, j] })
),
};
vec![Error {
path: base_path.to_string(),
code: "ONE_OF_VIOLATED".to_string(),
message,
cause,
}]
}
// 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
let mut unique_errors: HashMap<String, Value> = HashMap::new();
for error in errors {
if let Entry::Vacant(entry) = unique_errors.entry(error.path.clone()) {
// Extract the failing value from the instance
let failing_value = extract_value_at_path(instance, &error.path);
entry.insert(json!({
"code": error.code,
"message": error.message,
"details": {
"path": error.path,
"context": failing_value,
"cause": error.cause,
"schema": schema_id
}
}));
}
}
unique_errors.into_values().collect()
}
// Helper function to extract value at a JSON pointer path
fn extract_value_at_path(instance: &Value, path: &str) -> Value {
let parts: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
let mut current = instance;
for part in parts {
match current {
Value::Object(map) => {
if let Some(value) = map.get(part) {
current = value;
} else {
return Value::Null;
}
}
Value::Array(arr) => {
if let Ok(index) = part.parse::<usize>() {
if let Some(value) = arr.get(index) {
current = value;
} else {
return Value::Null;
}
} else {
return Value::Null;
}
}
_ => return Value::Null,
let parts: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
let mut current = instance;
for part in parts {
match current {
Value::Object(map) => {
if let Some(value) = map.get(part) {
current = value;
} else {
return Value::Null;
}
}
Value::Array(arr) => {
if let Ok(index) = part.parse::<usize>() {
if let Some(value) = arr.get(index) {
current = value;
} else {
return Value::Null;
}
} else {
return Value::Null;
}
}
_ => return Value::Null,
}
current.clone()
}
}
// Helper to convert validation messages to error codes and human-readable messages
fn enhance_error_message(message: &str) -> (String, String) {
// Match exact boon error message patterns
let trimmed = message.trim();
if trimmed.contains("value must be one of") {
("ENUM_VIOLATED".to_string(),
"Value is not one of the allowed options".to_string())
} else if trimmed.contains("length must be >=") && trimmed.contains("but got") {
("MIN_LENGTH_VIOLATED".to_string(),
"Field length is below the minimum required".to_string())
} else if trimmed.contains("length must be <=") && trimmed.contains("but got") {
("MAX_LENGTH_VIOLATED".to_string(),
"Field length exceeds the maximum allowed".to_string())
} else if trimmed.contains("must be >=") && trimmed.contains("but got") {
("MINIMUM_VIOLATED".to_string(),
"Value is below the minimum allowed".to_string())
} else if trimmed.contains("must be <=") && trimmed.contains("but got") {
("MAXIMUM_VIOLATED".to_string(),
"Value exceeds the maximum allowed".to_string())
} else if trimmed.contains("must be >") && trimmed.contains("but got") {
("EXCLUSIVE_MINIMUM_VIOLATED".to_string(),
"Value must be greater than the minimum".to_string())
} else if trimmed.contains("must be <") && trimmed.contains("but got") {
("EXCLUSIVE_MAXIMUM_VIOLATED".to_string(),
"Value must be less than the maximum".to_string())
} else if trimmed.contains("does not match pattern") {
("PATTERN_VIOLATED".to_string(),
"Value does not match the required pattern".to_string())
} else if trimmed.contains("missing properties") {
("REQUIRED_FIELD_MISSING".to_string(),
"Required field is missing".to_string())
} else if trimmed.contains("want") && trimmed.contains("but got") {
("TYPE_MISMATCH".to_string(),
"Field type does not match the expected type".to_string())
} else if trimmed.starts_with("value must be") && !trimmed.contains("one of") {
("CONST_VIOLATED".to_string(),
"Value does not match the required constant".to_string())
} else if trimmed.contains("is not valid") && trimmed.contains(":") {
("FORMAT_INVALID".to_string(),
extract_format_message(trimmed))
} else if trimmed.contains("items at") && trimmed.contains("are equal") {
("UNIQUE_ITEMS_VIOLATED".to_string(),
"Array contains duplicate items".to_string())
} else if trimmed.contains("additionalProperties") && trimmed.contains("not allowed") {
("ADDITIONAL_PROPERTIES_NOT_ALLOWED".to_string(),
"Object contains properties that are not allowed".to_string())
} else if trimmed.contains("is not multipleOf") {
("MULTIPLE_OF_VIOLATED".to_string(),
"Value is not a multiple of the required factor".to_string())
} else if trimmed.contains("minimum") && trimmed.contains("properties required") {
("MIN_PROPERTIES_VIOLATED".to_string(),
"Object has fewer properties than required".to_string())
} else if trimmed.contains("maximum") && trimmed.contains("properties required") {
("MAX_PROPERTIES_VIOLATED".to_string(),
"Object has more properties than allowed".to_string())
} else if trimmed.contains("minimum") && trimmed.contains("items required") {
("MIN_ITEMS_VIOLATED".to_string(),
"Array has fewer items than required".to_string())
} else if trimmed.contains("maximum") && trimmed.contains("items required") {
("MAX_ITEMS_VIOLATED".to_string(),
"Array has more items than allowed".to_string())
} else if trimmed == "false schema" {
("FALSE_SCHEMA".to_string(),
"Schema validation always fails".to_string())
} else if trimmed == "not failed" {
("NOT_VIOLATED".to_string(),
"Value matched a schema it should not match".to_string())
} else if trimmed == "allOf failed" {
("ALL_OF_VIOLATED".to_string(),
"Value does not match all required schemas".to_string())
} else if trimmed == "anyOf failed" {
("ANY_OF_VIOLATED".to_string(),
"Value does not match any of the allowed schemas".to_string())
} else if trimmed.contains("oneOf failed") {
("ONE_OF_VIOLATED".to_string(),
"Value must match exactly one schema".to_string())
} else if trimmed == "validation failed" {
("VALIDATION_FAILED".to_string(),
"Validation failed".to_string())
} else {
// For any unmatched patterns, try to provide a generic human-readable message
// while preserving the original error in details.cause
("VALIDATION_FAILED".to_string(),
"Validation failed".to_string())
}
}
// Extract a better format message
fn extract_format_message(message: &str) -> String {
if message.contains("date-time") {
"Invalid date-time format".to_string()
} else if message.contains("email") {
"Invalid email format".to_string()
} else if message.contains("uri") {
"Invalid URI format".to_string()
} else if message.contains("uuid") {
"Invalid UUID format".to_string()
} else {
"Invalid format".to_string()
}
current.clone()
}
#[pg_extern(strict, parallel_safe)]
@ -347,7 +797,6 @@ pub mod pg_test {
}
}
#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {

File diff suppressed because it is too large Load Diff

View File

@ -1 +1 @@
1.0.21
1.0.30