even more jspg improved error handling, missing some codes before

This commit is contained in:
2025-06-11 19:38:46 -04:00
parent 7f66a4a35a
commit 92c0a6fc0b
2 changed files with 254 additions and 203 deletions

1
rustfmt.toml Normal file
View File

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

View File

@ -2,16 +2,26 @@ use pgrx::*;
pg_module_magic!(); pg_module_magic!();
use serde_json::{json, Value}; use boon::{CompileError, Compiler, ErrorKind, SchemaIndex, Schemas, ValidationError};
use std::{collections::HashMap, sync::RwLock};
use boon::{Compiler, Schemas, ValidationError, SchemaIndex, CompileError};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use serde_json::{json, Value};
use std::collections::hash_map::Entry;
use std::{collections::HashMap, sync::RwLock};
struct BoonCache { struct BoonCache {
schemas: Schemas, schemas: Schemas,
id_to_index: HashMap<String, SchemaIndex>, id_to_index: HashMap<String, SchemaIndex>,
} }
// Structure to hold error information without lifetimes
#[derive(Debug)]
struct Error {
path: String,
code: String,
message: String,
cause: String,
}
lazy_static! { lazy_static! {
static ref SCHEMA_CACHE: RwLock<BoonCache> = RwLock::new(BoonCache { static ref SCHEMA_CACHE: RwLock<BoonCache> = RwLock::new(BoonCache {
schemas: Schemas::new(), schemas: Schemas::new(),
@ -30,6 +40,7 @@ fn cache_json_schema(schema_id: &str, schema: JsonB, strict: bool) -> JsonB {
apply_strict_validation(&mut schema_value); apply_strict_validation(&mut schema_value);
} }
// Create the boon compiler and enable format assertions
let mut compiler = Compiler::new(); let mut compiler = Compiler::new();
compiler.enable_format_assertions(); compiler.enable_format_assertions();
@ -59,9 +70,9 @@ fn cache_json_schema(schema_id: &str, schema: JsonB, strict: bool) -> JsonB {
CompileError::ValidationError { url: _url, src } => { CompileError::ValidationError { url: _url, src } => {
// Collect leaf errors from the meta-schema validation failure // Collect leaf errors from the meta-schema validation failure
let mut error_list = Vec::new(); 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 // Filter and format errors properly - no instance for schema compilation
format_drop_errors(error_list, &schema_value) format_errors(error_list, &schema_value)
} }
_ => { _ => {
// Other compilation errors // Other compilation errors
@ -90,7 +101,6 @@ fn apply_strict_validation(schema: &mut Value) {
map.insert("additionalProperties".to_string(), Value::Bool(false)); map.insert("additionalProperties".to_string(), Value::Bool(false));
} }
} }
// Recurse into all properties // Recurse into all properties
for (_, value) in map.iter_mut() { for (_, value) in map.iter_mut() {
apply_strict_validation(value); apply_strict_validation(value);
@ -109,7 +119,6 @@ fn apply_strict_validation(schema: &mut Value) {
#[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();
// Lookup uses the original schema_id // Lookup uses the original schema_id
match cache.id_to_index.get(schema_id) { match cache.id_to_index.get(schema_id) {
None => JsonB(json!({ None => JsonB(json!({
@ -127,9 +136,8 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
Ok(_) => JsonB(json!({ "response": "success" })), Ok(_) => JsonB(json!({ "response": "success" })),
Err(validation_error) => { Err(validation_error) => {
let mut error_list = Vec::new(); let mut error_list = Vec::new();
collect_validation_errors(&validation_error, &mut error_list); collect_errors(&validation_error, &mut error_list);
let errors = format_drop_errors(error_list, &instance_value); let errors = format_errors(error_list, &instance_value);
JsonB(json!({ "errors": errors })) JsonB(json!({ "errors": errors }))
} }
} }
@ -138,60 +146,206 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
} }
// Recursively collects validation errors // 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 // Check if this is a structural error that we should skip
let error_message = format!("{}", error.kind); let is_structural = matches!(
let is_structural = error_message == "validation failed" || &error.kind,
error_message == "allOf failed" || ErrorKind::Group | ErrorKind::AllOf | ErrorKind::AnyOf | ErrorKind::Not | ErrorKind::OneOf(_)
error_message == "anyOf failed" || );
error_message == "not failed" ||
error_message.starts_with("oneOf failed");
if error.causes.is_empty() && !is_structural { if error.causes.is_empty() && !is_structural {
// This is a leaf error that's not structural // This is a leaf error that's not structural
// Format just the error kind, not the whole validation error let original_message = format!("{}", error.kind);
let message = format!("{}", error.kind); let (error_code, human_message) = convert_error_kind(&error.kind);
errors_list.push(( errors_list.push(Error {
error.instance_location.to_string(), path: error.instance_location.to_string(),
error.schema_url.to_string(), code: error_code,
message message: human_message,
)); cause: original_message,
});
} else { } else {
// Recurse into causes // Recurse into causes
for cause in &error.causes { for cause in &error.causes {
collect_validation_errors(cause, errors_list); collect_errors(cause, errors_list);
} }
} }
} }
// Convert ErrorKind to error code and human message
fn convert_error_kind(kind: &ErrorKind) -> (String, String) {
match kind {
ErrorKind::Type { .. } => (
"TYPE_MISMATCH".to_string(),
"Field type does not match the expected type".to_string(),
),
ErrorKind::Required { .. } => (
"REQUIRED_FIELD_MISSING".to_string(),
"Required field is missing".to_string(),
),
ErrorKind::DependentRequired { .. } => (
"DEPENDENT_REQUIRED_MISSING".to_string(),
"Dependent required fields are missing".to_string(),
),
ErrorKind::Dependency { .. } => (
"DEPENDENCY_FAILED".to_string(),
"Dependency requirement not met".to_string(),
),
ErrorKind::Enum { .. } => (
"ENUM_VIOLATED".to_string(),
"Value is not one of the allowed options".to_string(),
),
ErrorKind::Const { .. } => (
"CONST_VIOLATED".to_string(),
"Value does not match the required constant".to_string(),
),
ErrorKind::MinLength { .. } => (
"MIN_LENGTH_VIOLATED".to_string(),
"Field length is below the minimum required".to_string(),
),
ErrorKind::MaxLength { .. } => (
"MAX_LENGTH_VIOLATED".to_string(),
"Field length exceeds the maximum allowed".to_string(),
),
ErrorKind::Pattern { .. } => (
"PATTERN_VIOLATED".to_string(),
"Value does not match the required pattern".to_string(),
),
ErrorKind::Minimum { .. } => (
"MINIMUM_VIOLATED".to_string(),
"Value is below the minimum allowed".to_string(),
),
ErrorKind::Maximum { .. } => (
"MAXIMUM_VIOLATED".to_string(),
"Value exceeds the maximum allowed".to_string(),
),
ErrorKind::ExclusiveMinimum { .. } => (
"EXCLUSIVE_MINIMUM_VIOLATED".to_string(),
"Value must be greater than the minimum".to_string(),
),
ErrorKind::ExclusiveMaximum { .. } => (
"EXCLUSIVE_MAXIMUM_VIOLATED".to_string(),
"Value must be less than the maximum".to_string(),
),
ErrorKind::MultipleOf { .. } => (
"MULTIPLE_OF_VIOLATED".to_string(),
"Value is not a multiple of the required factor".to_string(),
),
ErrorKind::MinItems { .. } => (
"MIN_ITEMS_VIOLATED".to_string(),
"Array has fewer items than required".to_string(),
),
ErrorKind::MaxItems { .. } => (
"MAX_ITEMS_VIOLATED".to_string(),
"Array has more items than allowed".to_string(),
),
ErrorKind::UniqueItems { .. } => (
"UNIQUE_ITEMS_VIOLATED".to_string(),
"Array contains duplicate items".to_string(),
),
ErrorKind::MinProperties { .. } => (
"MIN_PROPERTIES_VIOLATED".to_string(),
"Object has fewer properties than required".to_string(),
),
ErrorKind::MaxProperties { .. } => (
"MAX_PROPERTIES_VIOLATED".to_string(),
"Object has more properties than allowed".to_string(),
),
ErrorKind::AdditionalProperties { .. } => (
"ADDITIONAL_PROPERTIES_NOT_ALLOWED".to_string(),
"Object contains properties that are not allowed".to_string(),
),
ErrorKind::AdditionalItems { .. } => (
"ADDITIONAL_ITEMS_NOT_ALLOWED".to_string(),
"Array contains additional items that are not allowed".to_string(),
),
ErrorKind::Format { want, .. } => (
"FORMAT_INVALID".to_string(),
format!("Invalid {} format", want),
),
ErrorKind::PropertyName { .. } => (
"INVALID_PROPERTY_NAME".to_string(),
"Property name is invalid".to_string(),
),
ErrorKind::Contains => (
"CONTAINS_FAILED".to_string(),
"No items match the required schema".to_string(),
),
ErrorKind::MinContains { .. } => (
"MIN_CONTAINS_VIOLATED".to_string(),
"Too few items match the required schema".to_string(),
),
ErrorKind::MaxContains { .. } => (
"MAX_CONTAINS_VIOLATED".to_string(),
"Too many items match the required schema".to_string(),
),
ErrorKind::ContentEncoding { .. } => (
"CONTENT_ENCODING_INVALID".to_string(),
"Content encoding is invalid".to_string(),
),
ErrorKind::ContentMediaType { .. } => (
"CONTENT_MEDIA_TYPE_INVALID".to_string(),
"Content media type is invalid".to_string(),
),
ErrorKind::FalseSchema => (
"FALSE_SCHEMA".to_string(),
"Schema validation always fails".to_string(),
),
ErrorKind::Not => (
"NOT_VIOLATED".to_string(),
"Value matched a schema it should not match".to_string(),
),
ErrorKind::RefCycle { .. } => (
"REFERENCE_CYCLE".to_string(),
"Schema contains a reference cycle".to_string(),
),
ErrorKind::Reference { .. } => (
"REFERENCE_FAILED".to_string(),
"Reference validation failed".to_string(),
),
ErrorKind::Schema { .. } => (
"SCHEMA_FAILED".to_string(),
"Schema validation failed".to_string(),
),
ErrorKind::ContentSchema => (
"CONTENT_SCHEMA_FAILED".to_string(),
"Content schema validation failed".to_string(),
),
// These shouldn't appear as leaf errors due to is_structural check
ErrorKind::Group => (
"VALIDATION_FAILED".to_string(),
"Validation failed".to_string(),
),
ErrorKind::AllOf => (
"ALL_OF_VIOLATED".to_string(),
"Value does not match all required schemas".to_string(),
),
ErrorKind::AnyOf => (
"ANY_OF_VIOLATED".to_string(),
"Value does not match any of the allowed schemas".to_string(),
),
ErrorKind::OneOf(_) => (
"ONE_OF_VIOLATED".to_string(),
"Value must match exactly one schema".to_string(),
),
}
}
// Formats errors according to DropError structure // Formats errors according to DropError structure
fn format_drop_errors(raw_errors: Vec<(String, String, String)>, instance: &Value) -> Vec<Value> { fn format_errors(errors: Vec<Error>, instance: &Value) -> Vec<Value> {
use std::collections::HashMap; // Deduplicate by instance_path and format as DropError
use std::collections::hash_map::Entry;
// 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(); let mut unique_errors: HashMap<String, Value> = HashMap::new();
for (instance_path, _schema_path, message) in plausible_errors { for error in errors {
if let Entry::Vacant(entry) = unique_errors.entry(instance_path.clone()) { if let Entry::Vacant(entry) = unique_errors.entry(error.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 // Extract the failing value from the instance
let failing_value = extract_value_at_path(instance, &instance_path); let failing_value = extract_value_at_path(instance, &error.path);
entry.insert(json!({ entry.insert(json!({
"code": code, "code": error.code,
"message": human_message, "message": error.message,
"details": { "details": {
"path": instance_path, "path": error.path,
"context": failing_value, "context": failing_value,
"cause": message // Original error message "cause": error.cause
} }
})); }));
} }
@ -232,109 +386,6 @@ fn extract_value_at_path(instance: &Value, path: &str) -> Value {
current.clone() 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()
}
}
#[pg_extern(strict, parallel_safe)] #[pg_extern(strict, parallel_safe)]
fn json_schema_cached(schema_id: &str) -> bool { fn json_schema_cached(schema_id: &str) -> bool {
let cache = SCHEMA_CACHE.read().unwrap(); let cache = SCHEMA_CACHE.read().unwrap();
@ -373,7 +424,6 @@ pub mod pg_test {
} }
} }
#[cfg(any(test, feature = "pg_test"))] #[cfg(any(test, feature = "pg_test"))]
#[pg_schema] #[pg_schema]
mod tests { mod tests {