Compare commits

..

7 Commits

11 changed files with 527 additions and 51 deletions

View File

@ -70,6 +70,10 @@
"type": "string",
"format": "date-time"
},
"uuid_field": {
"type": "string",
"format": "uuid"
},
"tags": {
"type": "array",
"items": {
@ -181,6 +185,17 @@
]
}
}
},
"uuid.condition": {
"type": "condition",
"properties": {
"$eq": {
"type": [
"string",
"null"
]
}
}
}
}
}
@ -244,6 +259,7 @@
"billing_address",
"gender",
"birth_date",
"uuid_field",
"tags",
"ad_hoc",
"$and",
@ -258,6 +274,7 @@
"billing_address",
"gender",
"birth_date",
"uuid_field",
"tags",
"ad_hoc",
"$and",
@ -278,6 +295,7 @@
"billing_address",
"gender",
"birth_date",
"uuid_field",
"tags",
"ad_hoc",
"$and",
@ -325,6 +343,12 @@
"null"
]
},
"uuid_field": {
"type": [
"uuid.condition",
"null"
]
},
"first_name": {
"type": [
"string.condition",
@ -396,6 +420,7 @@
"string.condition": {},
"integer.condition": {},
"date.condition": {},
"uuid.condition": {},
"search": {},
"search.filter": {
"type": "filter",

View File

@ -2432,7 +2432,7 @@
" JOIN agreego.entity entity_2 ON entity_2.id = order_1.id",
" WHERE",
" NOT entity_2.archived",
" AND order_1.counterparty_type = 'organization'",
" AND order_1.counterparty_type IN ('bot', 'organization', 'person')",
"))))"
]
]

View File

@ -141,6 +141,8 @@ impl Schema {
if let Some(fmt) = &schema.obj.format {
if fmt == "date-time" {
return Some(vec!["date.condition".to_string()]);
} else if fmt == "uuid" {
return Some(vec!["uuid.condition".to_string()]);
}
}
Some(vec!["string.condition".to_string()])

View File

@ -603,7 +603,7 @@ impl<'a> Compiler<'a> {
if let Some(type_name) = bound_type_name {
// Ensure this type actually exists
if self.db.types.contains_key(&type_name) {
if let Some(type_def) = self.db.types.get(&type_name) {
if let Some(relation) = self.db.relations.get(&edge.constraint) {
let mut poly_col = None;
let mut table_to_alias = "";
@ -621,7 +621,23 @@ impl<'a> Compiler<'a> {
.get(table_to_alias)
.or_else(|| type_aliases.get(&node.parent_alias))
{
where_clauses.push(format!("{}.{} = '{}'", alias, col, type_name));
// DEBUG: See what variations we have for this type
#[cfg(not(test))]
pgrx::notice!("JSPG_POLY_BOUNDS: type={}, col={}, variations_len={}, variations={:?}", type_name, col, type_def.variations.len(), type_def.variations);
// Use IN clause with all variations to support inherited types.
// e.g., "asset" has variations ["asset", "property", "unit", ...]
// so target_type IN ('asset','property','unit') instead of = 'asset'
if type_def.variations.len() > 1 {
let quoted: Vec<String> = type_def.variations.iter()
.map(|v| format!("'{}'", v))
.collect();
where_clauses.push(format!(
"{}.{} IN ({})", alias, col, quoted.join(", ")
));
} else {
where_clauses.push(format!("{}.{} = '{}'", alias, col, type_name));
}
}
}
}

View File

@ -50,6 +50,10 @@ impl Queryer {
Err(drop) => return drop,
};
// DEBUG: Emit compiled SQL as a NOTICE for inspection
#[cfg(not(test))]
pgrx::notice!("JSPG_SQL[{}]: {}", schema_id, sql);
// 3. Execute via Database Executor
self.execute_sql(schema_id, &sql, args)
}

View File

@ -0,0 +1,177 @@
use crate::*;
use serde_json::json;
fn setup_dynamic_pointer_db() {
let db_json = json!({
"puncs": [],
"enums": [],
"relations": [],
"types": [
{
"name": "bid.evaluation_context",
"variations": ["bid.evaluation_context"],
"hierarchy": ["bid.evaluation_context", "entity"],
"schemas": {
"bid.evaluation_context": {
"type": "object",
"properties": {
"amount": { "type": "number" }
},
"required": ["amount"]
}
}
},
{
"name": "work_order.evaluation_context",
"variations": ["work_order.evaluation_context"],
"hierarchy": ["work_order.evaluation_context", "entity"],
"schemas": {
"work_order.evaluation_context": {
"type": "object",
"properties": {
"vendor_id": { "type": "string" }
},
"required": ["vendor_id"]
}
}
},
{
"name": "evaluation_rule",
"variations": ["evaluation_rule"],
"hierarchy": ["evaluation_rule", "entity"],
"schemas": {
"evaluation_rule": {
"type": "object",
"properties": {
"entity_type": { "type": "string" },
"evaluation_context": { "type": "$entity_type.evaluation_context" }
},
"required": ["entity_type", "evaluation_context"]
}
}
}
]
});
let setup_res = jspg_setup(Json(db_json));
assert_eq!(
setup_res.0,
json!({
"type": "drop",
"response": "success"
})
);
}
fn test_structural_inheritance_dynamic_pointer() {
setup_dynamic_pointer_db();
// 1. Happy path: entity_type = bid, evaluation_context matches bid.evaluation_context
let happy_bid = json!({
"entity_type": "bid",
"evaluation_context": {
"amount": 5000
}
});
let res_happy_bid = jspg_validate("evaluation_rule", JsonB(happy_bid));
assert_eq!(
res_happy_bid.0,
json!({
"type": "drop",
"response": "success"
})
);
// 2. Happy path: entity_type = work_order, evaluation_context matches work_order.evaluation_context
let happy_wo = json!({
"entity_type": "work_order",
"evaluation_context": {
"vendor_id": "vendor-123"
}
});
let res_happy_wo = jspg_validate("evaluation_rule", JsonB(happy_wo));
assert_eq!(
res_happy_wo.0,
json!({
"type": "drop",
"response": "success"
})
);
// 3. Unhappy path: entity_type = bid but evaluation_context has invalid schema (missing required field 'amount')
let unhappy_bid = json!({
"entity_type": "bid",
"evaluation_context": {
"vendor_id": "vendor-123"
}
});
let res_unhappy_bid = jspg_validate("evaluation_rule", JsonB(unhappy_bid));
assert_eq!(
res_unhappy_bid.0,
json!({
"type": "drop",
"errors": [
{
"code": "REQUIRED_FIELD_MISSING",
"message": "Missing amount",
"details": { "path": "evaluation_context/amount" }
},
{
"code": "STRICT_PROPERTY_VIOLATION",
"message": "Unexpected property 'vendor_id'",
"details": { "path": "evaluation_context/vendor_id" }
}
]
})
);
// 4. Missing discriminator: dynamic pointer cannot resolve property 'entity_type'
let missing_disc = json!({
"evaluation_context": {}
});
let res_missing_disc = jspg_validate("evaluation_rule", JsonB(missing_disc));
assert_eq!(
res_missing_disc.0,
json!({
"type": "drop",
"errors": [
{
"code": "REQUIRED_FIELD_MISSING",
"message": "Missing entity_type",
"details": { "path": "entity_type" }
},
{
"code": "DYNAMIC_TYPE_RESOLUTION_FAILED",
"message": "Dynamic type pointer '$entity_type.evaluation_context' could not resolve discriminator property 'entity_type' on parent instance",
"details": { "path": "evaluation_context" }
}
]
})
);
// 5. Unregistered discriminator: target not found in registry
let unknown_disc = json!({
"entity_type": "unknown",
"evaluation_context": {}
});
let res_unknown_disc = jspg_validate("evaluation_rule", JsonB(unknown_disc));
assert_eq!(
res_unknown_disc.0,
json!({
"type": "drop",
"errors": [
{
"code": "DYNAMIC_TYPE_RESOLUTION_FAILED",
"message": "Resolved dynamic type pointer 'unknown.evaluation_context' was not found in schema registry",
"details": { "path": "evaluation_context" }
}
]
})
);
let _ = jspg_teardown();
}
pub fn run_all() {
test_structural_inheritance_dynamic_pointer();
}

View File

@ -0,0 +1,124 @@
use crate::*;
use serde_json::json;
fn setup_simple_db() {
let db_json = json!({
"puncs": [],
"enums": [],
"relations": [],
"types": [
{
"name": "person",
"variations": ["person"],
"hierarchy": ["person", "entity"],
"schemas": {
"person": {
"type": "object",
"properties": {
"type": { "type": "string" },
"name": { "type": "string" }
},
"required": ["name"]
}
}
}
]
});
let setup_res = jspg_setup(Json(db_json));
assert_eq!(
setup_res.0,
json!({
"type": "drop",
"response": "success"
})
);
}
fn test_inline_schema_happy_path() {
// 1. Valid instance matching the inline schema
let inline_schema = r#"{"type": "object", "properties": {"age": {"type": "number"}}, "required": ["age"]}"#;
let happy_res = jspg_validate(inline_schema, JsonB(json!({"age": 42})));
assert_eq!(
happy_res.0,
json!({
"type": "drop",
"response": "success"
})
);
// 2. Invalid instance failing the inline schema (missing required field)
let unhappy_res = jspg_validate(inline_schema, JsonB(json!({"name": "Neo"})));
assert_eq!(
unhappy_res.0,
json!({
"type": "drop",
"errors": [
{
"code": "REQUIRED_FIELD_MISSING",
"message": "Missing age",
"details": { "path": "age" }
},
{
"code": "STRICT_PROPERTY_VIOLATION",
"message": "Unexpected property 'name'",
"details": { "path": "name" }
}
]
})
);
// 3. Inline schema referencing registered custom type "person"
let inline_with_ref = r#"{"type": "object", "properties": {"who": {"type": "person"}}, "required": ["who"]}"#;
let happy_ref_res = jspg_validate(inline_with_ref, JsonB(json!({"who": {"type": "person", "name": "Morpheus"}})));
assert_eq!(
happy_ref_res.0,
json!({
"type": "drop",
"response": "success"
})
);
let unhappy_ref_res = jspg_validate(inline_with_ref, JsonB(json!({"who": {"type": "person"}})));
assert_eq!(
unhappy_ref_res.0,
json!({
"type": "drop",
"errors": [
{
"code": "REQUIRED_FIELD_MISSING",
"message": "Missing name",
"details": { "path": "who/name" }
}
]
})
);
}
fn test_inline_schema_syntax_errors() {
let malformed_schema = r#"{"type": "object", "properties": {"name": {"type": "#;
let res = jspg_validate(malformed_schema, JsonB(json!({})));
let errors = res.0.get("errors").expect("Expected errors block");
let err = errors.get(0).expect("Expected at least one error");
assert_eq!(err.get("code").unwrap().as_str().unwrap(), "SCHEMA_PARSE_FAILED");
assert!(err.get("message").unwrap().as_str().unwrap().contains("Failed to parse inline schema"));
}
fn test_inline_schema_ref_compilation_errors() {
// Try to use a custom type that is not defined in the compiled database context.
let invalid_ref_schema = r#"{"type": "object", "properties": {"item": {"type": "non_existent_type"}}}"#;
let res = jspg_validate(invalid_ref_schema, JsonB(json!({"item": {}})));
let errors = res.0.get("errors").expect("Expected errors block");
let err = errors.get(0).expect("Expected compilation error");
assert_eq!(err.get("code").unwrap().as_str().unwrap(), "PROXY_TYPE_RESOLUTION_FAILED");
assert!(err.get("message").unwrap().as_str().unwrap().contains("non_existent_type"));
}
pub fn run_all() {
setup_simple_db();
test_inline_schema_happy_path();
test_inline_schema_syntax_errors();
test_inline_schema_ref_compilation_errors();
let _ = jspg_teardown();
}

View File

@ -2,12 +2,28 @@ use crate::*;
pub mod formatter;
pub mod runner;
pub mod types;
pub mod inline_validator;
pub mod swap_concurrency;
pub mod inheritance_matching;
use serde_json::json;
lazy_static::lazy_static! {
pub static ref TEST_MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
}
// Database module tests moved to src/database/executors/mock.rs
#[test]
fn test_jspg_custom_audit_suite() {
let _guard = TEST_MUTEX.lock().unwrap();
inline_validator::run_all();
inheritance_matching::run_all();
swap_concurrency::run_all();
}
#[test]
fn test_library_api() {
let _guard = TEST_MUTEX.lock().unwrap();
// 1. Initially, schemas are not cached.
// Expected uninitialized drop format: errors + null response

View File

@ -0,0 +1,91 @@
use crate::*;
use serde_json::json;
use std::thread;
use std::sync::Arc;
fn get_concurrency_db() -> serde_json::Value {
json!({
"puncs": [],
"enums": [],
"relations": [],
"types": [
{
"name": "person",
"variations": ["person"],
"hierarchy": ["person", "entity"],
"schemas": {
"person": {
"type": "object",
"properties": {
"type": { "type": "string" },
"name": { "type": "string" }
},
"required": ["name"]
}
}
}
]
})
}
fn test_concurrent_atomic_swap_isolation() {
// 1. Initialize first
let db_json = get_concurrency_db();
let setup_res = jspg_setup(Json(db_json.clone()));
assert_eq!(
setup_res.0,
json!({
"type": "drop",
"response": "success"
})
);
// 2. Spawn 8 parallel reader threads validating schemas
let num_readers = 8;
let mut handles = Vec::new();
for i in 0..num_readers {
handles.push(thread::spawn(move || {
let payload = json!({"type": "person", "name": format!("Neo-{}", i)});
for _ in 0..100 {
let res = jspg_validate("person", JsonB(payload.clone()));
// The validation either succeeds or returns ENGINE_NOT_INITIALIZED (if teardown just swapped it)
// BUT it must NEVER panic, deadlock, or return corrupt memory!
let val = res.0;
let is_success = val.get("response").and_then(|r| r.as_str()) == Some("success");
let is_uninit = val.get("errors")
.and_then(|e| e.as_array())
.and_then(|arr| arr.get(0))
.and_then(|err| err.get("code"))
.and_then(|c| c.as_str()) == Some("ENGINE_NOT_INITIALIZED");
assert!(is_success || is_uninit, "Unexpected validation response: {:?}", val);
thread::yield_now();
}
}));
}
// 3. Spawn a concurrent writer thread constantly calling setup and teardown
let db_json_shared = Arc::new(db_json);
let writer_handle = thread::spawn(move || {
for _ in 0..50 {
let _ = jspg_teardown();
thread::yield_now();
let _ = jspg_setup(Json((*db_json_shared).clone()));
thread::yield_now();
}
});
// 4. Join everyone and verify no thread panicked
for handle in handles {
handle.join().expect("Reader thread panicked or deadlocked!");
}
writer_handle.join().expect("Writer thread panicked or deadlocked!");
// Clean up
let _ = jspg_teardown();
}
pub fn run_all() {
test_concurrent_atomic_swap_isolation();
}

View File

@ -42,62 +42,83 @@ impl Validator {
}
pub fn validate(&self, schema_id: &str, instance: &Value) -> crate::drop::Drop {
let schema_opt = self.db.schemas.get(schema_id);
if let Some(schema) = schema_opt {
let ctx = ValidationContext::new(
&self.db,
&schema,
&schema,
instance,
HashSet::new(),
false,
false,
);
match ctx.validate_scoped() {
Ok(result) => {
if result.is_valid() {
crate::drop::Drop::success()
} else {
let errors: Vec<crate::drop::Error> = result
.errors
.into_iter()
.map(|e| crate::drop::Error {
code: e.code,
message: e.message,
details: crate::drop::ErrorDetails {
path: Some(e.path),
cause: None,
context: None,
schema: None,
},
})
.collect();
crate::drop::Drop::with_errors(errors)
let schema_arc = if schema_id.trim().starts_with('{') {
match serde_json::from_str::<crate::database::schema::Schema>(schema_id) {
Ok(schema) => {
let mut errors = Vec::new();
schema.compile(&self.db, "inline", "inline".to_string(), &mut errors);
if !errors.is_empty() {
return crate::drop::Drop::with_errors(errors);
}
Arc::new(schema)
}
Err(e) => {
return crate::drop::Drop::with_errors(vec![crate::drop::Error {
code: "SCHEMA_PARSE_FAILED".to_string(),
message: format!("Failed to parse inline schema: {}", e),
details: crate::drop::ErrorDetails::default(),
}]);
}
Err(e) => crate::drop::Drop::with_errors(vec![crate::drop::Error {
code: e.code,
message: e.message,
details: crate::drop::ErrorDetails {
path: Some(e.path),
cause: None,
context: None,
schema: None,
},
}]),
}
} else {
crate::drop::Drop::with_errors(vec![crate::drop::Error {
code: "SCHEMA_NOT_FOUND".to_string(),
message: format!("Schema {} not found", schema_id),
match self.db.schemas.get(schema_id) {
Some(schema) => Arc::clone(schema),
None => {
return crate::drop::Drop::with_errors(vec![crate::drop::Error {
code: "SCHEMA_NOT_FOUND".to_string(),
message: format!("Schema {} not found", schema_id),
details: crate::drop::ErrorDetails {
path: Some("/".to_string()),
cause: None,
context: None,
schema: None,
},
}]);
}
}
};
let ctx = ValidationContext::new(
&self.db,
&schema_arc,
&schema_arc,
instance,
HashSet::new(),
false,
false,
);
match ctx.validate_scoped() {
Ok(result) => {
if result.is_valid() {
crate::drop::Drop::success()
} else {
let errors: Vec<crate::drop::Error> = result
.errors
.into_iter()
.map(|e| crate::drop::Error {
code: e.code,
message: e.message,
details: crate::drop::ErrorDetails {
path: Some(e.path),
cause: None,
context: None,
schema: None,
},
})
.collect();
crate::drop::Drop::with_errors(errors)
}
}
Err(e) => crate::drop::Drop::with_errors(vec![crate::drop::Error {
code: e.code,
message: e.message,
details: crate::drop::ErrorDetails {
path: Some("/".to_string()),
path: Some(e.path),
cause: None,
context: None,
schema: None,
},
}])
}]),
}
}
}

View File

@ -1 +1 @@
1.0.148
1.0.151