Compare commits

...

4 Commits

17 changed files with 1046 additions and 650 deletions

View File

@ -234,7 +234,7 @@ The Queryer transforms Postgres into a pre-compiled Semantic Query Engine, desig
* **Dynamic Filtering**: Binds parameters natively through `cue.filters` objects. The queryer enforces a strict, structured, MongoDB-style operator syntax to map incoming JSON request constraints directly to their originating structural table columns. Filters support both flat path notation (e.g., `"contacts/is_primary": {...}`) and deeply nested recursive JSON structures (e.g., `{"contacts": {"is_primary": {...}}}`). The queryer recursively traverses and flattens these structures at AST compilation time.
* **Equality / Inequality**: `{"$eq": value}`, `{"$ne": value}` automatically map to `=` and `!=`.
* **Comparison**: `{"$gt": ...}`, `{"$gte": ...}`, `{"$lt": ...}`, `{"$lte": ...}` directly compile to Postgres comparison operators (`> `, `>=`, `<`, `<=`).
* **Array Inclusion**: `{"$in": [values]}`, `{"$nin": [values]}` use native `jsonb_array_elements_text()` bindings to enforce `IN` and `NOT IN` logic without runtime SQL injection risks.
* **Array Inclusion**: `{"$of": [values]}`, `{"$nof": [values]}` use native `jsonb_array_elements_text()` bindings to enforce `IN` and `NOT IN` logic without runtime SQL injection risks.
* **Text Matching (ILIKE)**: Evaluates `$eq` or `$ne` against string fields containing the `%` character natively into Postgres `ILIKE` and `NOT ILIKE` partial substring matches.
* **Type Casting**: Safely resolves dynamic combinations by casting values instantly into the physical database types mapped in the schema (e.g. parsing `uuid` bindings to `::uuid`, formatting DateTimes to `::timestamptz`, and numbers to `::numeric`).
* **Polymorphic SQL Generation (`family`)**: Compiles `family` properties by analyzing the **Physical Database Variations**, *not* the schema descendants.

View File

@ -651,16 +651,21 @@
"action": "compile",
"expect": {
"success": true,
"schemas": [
"full.contact",
"full.person",
"full.person/ad_hoc_bubble",
"full.person/extended_relations",
"full.person/extended_relations/target",
"light.email_address",
"some_bubble",
"student.person"
]
"schemas": {
"full.contact": {},
"full.contact.filter": {},
"full.person": {},
"full.person.filter": {},
"full.person/ad_hoc_bubble": {},
"full.person/extended_relations": {},
"full.person/extended_relations/target": {},
"light.email_address": {},
"light.email_address.filter": {},
"some_bubble": {},
"some_bubble.filter": {},
"student.person": {},
"student.person.filter": {}
}
}
}
]
@ -919,11 +924,14 @@
"action": "compile",
"expect": {
"success": true,
"schemas": [
"entity",
"invoice",
"invoice_line"
]
"schemas": {
"entity": {},
"entity.filter": {},
"invoice": {},
"invoice.filter": {},
"invoice_line": {},
"invoice_line.filter": {}
}
}
}
]

222
fixtures/filter.json Normal file
View File

@ -0,0 +1,222 @@
[
{
"description": "Filter Synthesis Object-Oriented Composition",
"database": {
"puncs": [],
"enums": [],
"relations": [
{
"id": "rel1",
"type": "relation",
"constraint": "fk_person_billing_address",
"source_type": "person",
"source_columns": [
"billing_address_id"
],
"destination_type": "address",
"destination_columns": [
"id"
],
"prefix": "billing_address"
}
],
"types": [
{
"id": "type1",
"type": "type",
"name": "person",
"module": "core",
"source": "person",
"hierarchy": [
"person"
],
"variations": [
"person"
],
"schemas": {
"person": {
"type": "object",
"properties": {
"first_name": {
"type": "string"
},
"age": {
"type": "integer"
},
"billing_address": {
"type": "address"
},
"birth_date": {
"type": "string",
"format": "date-time"
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"ad_hoc": {
"type": "object",
"properties": {
"foo": {
"type": "string"
}
}
}
}
}
}
},
{
"id": "type2",
"type": "type",
"name": "address",
"module": "core",
"source": "address",
"hierarchy": [
"address"
],
"variations": [
"address"
],
"schemas": {
"address": {
"type": "object",
"properties": {
"city": {
"type": "string"
}
}
}
}
},
{
"id": "type3",
"type": "type",
"name": "filter",
"module": "core",
"source": "filter",
"hierarchy": [
"filter"
],
"variations": [
"filter",
"string.condition",
"integer.condition",
"date.condition"
],
"schemas": {
"condition": {
"type": "object",
"properties": {
"kind": {
"type": "string"
}
}
},
"string.condition": {
"type": "condition",
"properties": {
"$eq": {
"type": [
"string",
"null"
]
}
}
},
"integer.condition": {
"type": "condition",
"properties": {
"$eq": {
"type": [
"integer",
"null"
]
}
}
},
"date.condition": {
"type": "condition",
"properties": {
"$eq": {
"type": [
"string",
"null"
]
}
}
}
}
}
]
},
"tests": [
{
"description": "Assert filter generation map accurately represents strongly typed conditions natively.",
"action": "compile",
"expect": {
"success": true,
"schemas": {
"person": {},
"person.filter": {
"type": "object",
"compiledPropertyNames": [
"age",
"billing_address",
"birth_date",
"first_name"
],
"properties": {
"first_name": {
"type": [
"string.condition",
"null"
]
},
"age": {
"type": [
"integer.condition",
"null"
]
},
"billing_address": {
"type": [
"address.filter",
"null"
]
},
"birth_date": {
"type": [
"date.condition",
"null"
]
}
}
},
"address": {},
"address.filter": {
"type": "object",
"compiledPropertyNames": [
"city"
],
"properties": {
"city": {
"type": [
"string.condition",
"null"
]
}
}
},
"condition": {},
"string.condition": {},
"integer.condition": {},
"date.condition": {}
}
}
}
]
}
]

View File

@ -1199,10 +1199,10 @@
"id": {
"$eq": "123e4567-e89b-12d3-a456-426614174000",
"$ne": "123e4567-e89b-12d3-a456-426614174001",
"$in": [
"$of": [
"123e4567-e89b-12d3-a456-426614174000"
],
"$nin": [
"$nof": [
"123e4567-e89b-12d3-a456-426614174001"
]
},
@ -1241,9 +1241,9 @@
" AND entity_1.created_at <= ($7#>>'{}')::timestamptz",
" AND entity_1.created_at != ($8#>>'{}')::timestamptz",
" AND entity_1.id = ($9#>>'{}')::uuid",
" AND entity_1.id IN (SELECT value::uuid FROM jsonb_array_elements_text(($10#>>'{}')::jsonb))",
" AND entity_1.id != ($11#>>'{}')::uuid",
" AND entity_1.id NOT IN (SELECT value::uuid FROM jsonb_array_elements_text(($12#>>'{}')::jsonb))",
" AND entity_1.id != ($10#>>'{}')::uuid",
" AND entity_1.id NOT IN (SELECT value::uuid FROM jsonb_array_elements_text(($11#>>'{}')::jsonb))",
" AND entity_1.id IN (SELECT value::uuid FROM jsonb_array_elements_text(($12#>>'{}')::jsonb))",
")))"
]
]
@ -1448,14 +1448,14 @@
"$eq": 30,
"$gt": 20,
"$gte": 20,
"$in": [
"$of": [
30,
40
],
"$lt": 50,
"$lte": 50,
"$ne": 25,
"$nin": [
"$nof": [
1,
2
]
@ -1481,24 +1481,24 @@
"$eq": "Jane%",
"$gt": "A",
"$gte": "A",
"$in": [
"$of": [
"Jane",
"John"
],
"$lt": "Z",
"$lte": "Z",
"$ne": "Doe",
"$nin": [
"$nof": [
"Bob"
]
},
"id": {
"$eq": "00000000-0000-0000-0000-000000000001",
"$in": [
"$of": [
"00000000-0000-0000-0000-000000000001"
],
"$ne": "00000000-0000-0000-0000-000000000002",
"$nin": [
"$nof": [
"00000000-0000-0000-0000-000000000002"
]
},
@ -1677,11 +1677,11 @@
" AND person_1.age = ($1#>>'{}')::numeric",
" AND person_1.age > ($2#>>'{}')::numeric",
" AND person_1.age >= ($3#>>'{}')::numeric",
" AND person_1.age IN (SELECT value::numeric FROM jsonb_array_elements_text(($4#>>'{}')::jsonb))",
" AND person_1.age < ($5#>>'{}')::numeric",
" AND person_1.age <= ($6#>>'{}')::numeric",
" AND person_1.age != ($7#>>'{}')::numeric",
" AND person_1.age NOT IN (SELECT value::numeric FROM jsonb_array_elements_text(($8#>>'{}')::jsonb))",
" AND person_1.age < ($4#>>'{}')::numeric",
" AND person_1.age <= ($5#>>'{}')::numeric",
" AND person_1.age != ($6#>>'{}')::numeric",
" AND person_1.age NOT IN (SELECT value::numeric FROM jsonb_array_elements_text(($7#>>'{}')::jsonb))",
" AND person_1.age IN (SELECT value::numeric FROM jsonb_array_elements_text(($8#>>'{}')::jsonb))",
" AND entity_3.archived = ($9#>>'{}')::boolean",
" AND entity_3.archived != ($10#>>'{}')::boolean",
" AND entity_3.created_at = ($12#>>'{}')::timestamptz",
@ -1693,15 +1693,15 @@
" AND person_1.first_name ILIKE $18#>>'{}'",
" AND person_1.first_name > ($19#>>'{}')",
" AND person_1.first_name >= ($20#>>'{}')",
" AND person_1.first_name IN (SELECT value FROM jsonb_array_elements_text(($21#>>'{}')::jsonb))",
" AND person_1.first_name < ($22#>>'{}')",
" AND person_1.first_name <= ($23#>>'{}')",
" AND person_1.first_name NOT ILIKE $24#>>'{}'",
" AND person_1.first_name NOT IN (SELECT value FROM jsonb_array_elements_text(($25#>>'{}')::jsonb))",
" AND person_1.first_name < ($21#>>'{}')",
" AND person_1.first_name <= ($22#>>'{}')",
" AND person_1.first_name NOT ILIKE $23#>>'{}'",
" AND person_1.first_name NOT IN (SELECT value FROM jsonb_array_elements_text(($24#>>'{}')::jsonb))",
" AND person_1.first_name IN (SELECT value FROM jsonb_array_elements_text(($25#>>'{}')::jsonb))",
" AND entity_3.id = ($26#>>'{}')::uuid",
" AND entity_3.id IN (SELECT value::uuid FROM jsonb_array_elements_text(($27#>>'{}')::jsonb))",
" AND entity_3.id != ($28#>>'{}')::uuid",
" AND entity_3.id NOT IN (SELECT value::uuid FROM jsonb_array_elements_text(($29#>>'{}')::jsonb))",
" AND entity_3.id != ($27#>>'{}')::uuid",
" AND entity_3.id NOT IN (SELECT value::uuid FROM jsonb_array_elements_text(($28#>>'{}')::jsonb))",
" AND entity_3.id IN (SELECT value::uuid FROM jsonb_array_elements_text(($29#>>'{}')::jsonb))",
" AND person_1.last_name ILIKE $30#>>'{}'",
" AND person_1.last_name NOT ILIKE $31#>>'{}')))"
]

View File

@ -0,0 +1,163 @@
use crate::database::schema::Schema;
use std::sync::Arc;
impl Schema {
#[allow(unused_variables)]
pub(crate) fn validate_identifier(
id: &str,
field_name: &str,
root_id: &str,
path: &str,
errors: &mut Vec<crate::drop::Error>,
) {
#[cfg(not(test))]
for c in id.chars() {
if !c.is_ascii_lowercase() && !c.is_ascii_digit() && c != '_' && c != '.' {
errors.push(crate::drop::Error {
code: "INVALID_IDENTIFIER".to_string(),
message: format!(
"Invalid character '{}' in JSON Schema '{}' property: '{}'. Identifiers must exclusively contain [a-z0-9_.]",
c, field_name, id
),
details: crate::drop::ErrorDetails {
path: Some(path.to_string()),
schema: Some(root_id.to_string()),
..Default::default()
},
});
return;
}
}
}
pub fn collect_schemas(
schema_arc: &Arc<Schema>,
root_id: &str,
path: String,
to_insert: &mut Vec<(String, Arc<Schema>)>,
errors: &mut Vec<crate::drop::Error>,
) {
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) = &schema_arc.obj.type_ {
if t == "array" {
if let Some(items) = &schema_arc.obj.items {
if let Some(crate::database::object::SchemaTypeOrArray::Single(it)) = &items.obj.type_ {
if !crate::database::object::is_primitive_type(it) {
if items.obj.properties.is_some() || items.obj.cases.is_some() {
to_insert.push((path.clone(), Arc::clone(schema_arc)));
}
}
}
}
} else if !crate::database::object::is_primitive_type(t) {
Self::validate_identifier(t, "type", root_id, &path, errors);
// Is this an explicit inline ad-hoc composition?
if schema_arc.obj.properties.is_some() || schema_arc.obj.cases.is_some() {
to_insert.push((path.clone(), Arc::clone(schema_arc)));
}
}
}
if let Some(family) = &schema_arc.obj.family {
Self::validate_identifier(family, "family", root_id, &path, errors);
}
Self::collect_child_schemas(schema_arc, root_id, path, to_insert, errors);
}
pub fn collect_child_schemas(
schema_arc: &Arc<Schema>,
root_id: &str,
path: String,
to_insert: &mut Vec<(String, Arc<Schema>)>,
errors: &mut Vec<crate::drop::Error>,
) {
if let Some(props) = &schema_arc.obj.properties {
for (k, v) in props.iter() {
let next_path = format!("{}/{}", path, k);
Self::collect_schemas(v, root_id, next_path, to_insert, errors);
}
}
if let Some(pattern_props) = &schema_arc.obj.pattern_properties {
for (k, v) in pattern_props.iter() {
let next_path = format!("{}/{}", path, k);
Self::collect_schemas(v, root_id, next_path, to_insert, errors);
}
}
let mut map_arr = |arr: &Vec<Arc<Schema>>, sub: &str| {
for (i, v) in arr.iter().enumerate() {
Self::collect_schemas(
v,
root_id,
format!("{}/{}/{}", path, sub, i),
to_insert,
errors,
);
}
};
if let Some(arr) = &schema_arc.obj.prefix_items {
map_arr(arr, "prefixItems");
}
if let Some(arr) = &schema_arc.obj.one_of {
map_arr(arr, "oneOf");
}
let mut map_opt = |opt: &Option<Arc<Schema>>, pass_path: bool, sub: &str| {
if let Some(v) = opt {
if pass_path {
// Arrays explicitly push their wrapper natively.
// 'items' becomes a transparent conduit, bypassing self-promotion and skipping the '/items' suffix.
Self::collect_child_schemas(v, root_id, path.clone(), to_insert, errors);
} else {
Self::collect_child_schemas(v, root_id, format!("{}/{}", path, sub), to_insert, errors);
}
}
};
map_opt(
&schema_arc.obj.additional_properties,
false,
"additionalProperties",
);
map_opt(&schema_arc.obj.items, true, "items");
map_opt(&schema_arc.obj.not, false, "not");
map_opt(&schema_arc.obj.contains, false, "contains");
map_opt(&schema_arc.obj.property_names, false, "propertyNames");
if let Some(cases) = &schema_arc.obj.cases {
for (i, c) in cases.iter().enumerate() {
if let Some(when) = &c.when {
Self::collect_schemas(
when,
root_id,
format!("{}/cases/{}/when", path, i),
to_insert,
errors,
);
}
if let Some(then) = &c.then {
Self::collect_schemas(
then,
root_id,
format!("{}/cases/{}/then", path, i),
to_insert,
errors,
);
}
if let Some(else_) = &c.else_ {
Self::collect_schemas(
else_,
root_id,
format!("{}/cases/{}/else", path, i),
to_insert,
errors,
);
}
}
}
}
}

View File

@ -0,0 +1,128 @@
use crate::database::schema::Schema;
impl Schema {
/// Dynamically infers and compiles all structural database relationships between this Schema
/// and its nested children. This functions recursively traverses the JSON Schema abstract syntax
/// tree, identifies physical PostgreSQL table boundaries, and locks the resulting relation
/// constraint paths directly onto the `compiled_edges` map in O(1) memory.
pub fn compile_edges(
&self,
db: &crate::database::Database,
root_id: &str,
path: &str,
props: &std::collections::BTreeMap<String, std::sync::Arc<Schema>>,
errors: &mut Vec<crate::drop::Error>,
) -> std::collections::BTreeMap<String, crate::database::edge::Edge> {
let mut schema_edges = std::collections::BTreeMap::new();
// Determine the physical Database Table Name this schema structurally represents
// Plucks the polymorphic discriminator via dot-notation (e.g. extracting "person" from "full.person")
let mut parent_type_name = None;
if let Some(family) = &self.obj.family {
// 1. Explicit horizontal routing
parent_type_name = Some(family.split('.').next_back().unwrap_or(family).to_string());
} else if path == root_id {
// 2. Root nodes trust their exact registry footprint
let base_type_name = path.split('.').next_back().unwrap_or(path).to_string();
if db.types.contains_key(&base_type_name) {
parent_type_name = Some(base_type_name);
}
} else if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) = &self.obj.type_ {
// 3. Nested graphs trust their explicit struct pointer reference
if !crate::database::object::is_primitive_type(t) {
parent_type_name = Some(t.split('.').next_back().unwrap_or(t).to_string());
}
}
if let Some(p_type) = parent_type_name {
// Proceed only if the resolved table physically exists within the Postgres Type hierarchy
if let Some(type_def) = db.types.get(&p_type) {
// Iterate over all discovered schema boundaries mapped inside the object
for (prop_name, prop_schema) in props {
let mut child_type_name = None;
let mut target_schema = prop_schema.clone();
let mut is_array = false;
// Structurally unpack the inner target entity if the object maps to an array list
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) =
&prop_schema.obj.type_
{
if t == "array" {
is_array = true;
if let Some(items) = &prop_schema.obj.items {
target_schema = items.clone();
}
}
}
// Determine the physical Postgres table backing the nested child schema recursively
if let Some(family) = &target_schema.obj.family {
child_type_name = Some(family.split('.').next_back().unwrap_or(family).to_string());
} else if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) =
&target_schema.obj.type_
{
if !crate::database::object::is_primitive_type(t) {
child_type_name = Some(t.split('.').next_back().unwrap_or(t).to_string());
}
} else if let Some(arr) = &target_schema.obj.one_of {
if let Some(first) = arr.first() {
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) = &first.obj.type_
{
if !crate::database::object::is_primitive_type(t) {
child_type_name = Some(t.split('.').next_back().unwrap_or(t).to_string());
}
}
}
}
if let Some(c_type) = child_type_name {
// Skip edge compilation for JSONB columns — they store data inline, not relationally.
// The physical column type from field_types is the single source of truth.
if let Some(ft) = type_def
.field_types
.as_ref()
.and_then(|v| v.get(prop_name.as_str()))
.and_then(|v| v.as_str())
{
if ft == "jsonb" {
continue;
}
}
if db.types.contains_key(&c_type) {
// Ensure the child Schema's AST has accurately compiled its own physical property keys so we can
// inject them securely for Many-to-Many Twin Deduction disambiguation matching.
target_schema.compile(db, root_id, format!("{}/{}", path, prop_name), errors);
if let Some(compiled_target_props) = target_schema.obj.compiled_properties.get() {
let keys_for_ambiguity: Vec<String> =
compiled_target_props.keys().cloned().collect();
// Interrogate the Database catalog graph to discover the exact Foreign Key Constraint connecting the components
if let Some((relation, is_forward)) = db.resolve_relation(
&p_type,
&c_type,
prop_name,
Some(&keys_for_ambiguity),
is_array,
Some(root_id),
&format!("{}/{}", path, prop_name),
errors,
) {
schema_edges.insert(
prop_name.clone(),
crate::database::edge::Edge {
constraint: relation.constraint.clone(),
forward: is_forward,
},
);
}
}
}
}
}
}
}
schema_edges
}
}

View File

@ -0,0 +1,78 @@
use crate::database::object::{SchemaObject, SchemaTypeOrArray};
use crate::database::schema::Schema;
use crate::database::Database;
use std::collections::BTreeMap;
use std::sync::Arc;
impl Schema {
pub fn compile_filter(
&self,
_db: &Database,
_root_id: &str,
_errors: &mut Vec<crate::drop::Error>,
) -> Option<Schema> {
if let Some(props) = self.obj.compiled_properties.get() {
let mut filter_props = BTreeMap::new();
for (key, child) in props {
if let Some(mut filter_type) = Self::resolve_filter_type(child) {
filter_type.push("null".to_string());
let mut child_obj = SchemaObject::default();
child_obj.type_ = Some(SchemaTypeOrArray::Multiple(filter_type));
filter_props.insert(key.clone(), Arc::new(Schema { obj: child_obj, always_fail: false }));
}
}
if !filter_props.is_empty() {
let mut wrapper_obj = SchemaObject::default();
wrapper_obj.type_ = Some(SchemaTypeOrArray::Single("object".to_string()));
wrapper_obj.properties = Some(filter_props);
return Some(Schema { obj: wrapper_obj, always_fail: false });
}
}
None
}
fn resolve_filter_type(schema: &Arc<Schema>) -> Option<Vec<String>> {
if let Some(type_) = &schema.obj.type_ {
match type_ {
SchemaTypeOrArray::Single(t) => {
return Self::map_filter_string(t, schema);
}
SchemaTypeOrArray::Multiple(types) => {
for t in types {
if t != "null" {
return Self::map_filter_string(t, schema);
}
}
}
}
}
None
}
fn map_filter_string(t: &str, schema: &Arc<Schema>) -> Option<Vec<String>> {
match t {
"string" => {
if let Some(fmt) = &schema.obj.format {
if fmt == "date-time" {
return Some(vec!["date.condition".to_string()]);
}
}
Some(vec!["string.condition".to_string()])
}
"integer" => Some(vec!["integer.condition".to_string()]),
"number" => Some(vec!["number.condition".to_string()]),
"boolean" => Some(vec!["boolean.condition".to_string()]),
"object" => None, // Inline structures are ignored in Composed References
"array" => None, // We don't filter primitive arrays or map complex arrays yet
"null" => None,
custom => {
// Assume anything else is a Relational cross-boundary that already has its own .filter dynamically built
Some(vec![format!("{}.filter", custom)])
}
}
}
}

175
src/database/compile/mod.rs Normal file
View File

@ -0,0 +1,175 @@
pub mod collection;
pub mod edges;
pub mod filters;
pub mod polymorphism;
use crate::database::schema::Schema;
impl Schema {
pub fn compile(
&self,
db: &crate::database::Database,
root_id: &str,
path: String,
errors: &mut Vec<crate::drop::Error>,
) {
if self.obj.compiled_properties.get().is_some() {
return;
}
if let Some(format_str) = &self.obj.format {
if let Some(fmt) = crate::database::formats::FORMATS.get(format_str.as_str()) {
let _ = self
.obj
.compiled_format
.set(crate::database::object::CompiledFormat::Func(fmt.func));
}
}
if let Some(pattern_str) = &self.obj.pattern {
if let Ok(re) = regex::Regex::new(pattern_str) {
let _ = self
.obj
.compiled_pattern
.set(crate::database::object::CompiledRegex(re));
}
}
if let Some(pattern_props) = &self.obj.pattern_properties {
let mut compiled = Vec::new();
for (k, v) in pattern_props {
if let Ok(re) = regex::Regex::new(k) {
compiled.push((crate::database::object::CompiledRegex(re), v.clone()));
}
}
if !compiled.is_empty() {
let _ = self.obj.compiled_pattern_properties.set(compiled);
}
}
let mut props = std::collections::BTreeMap::new();
// 1. Resolve INHERITANCE dependencies first
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) = &self.obj.type_ {
if !crate::database::object::is_primitive_type(t) {
if let Some(parent) = db.schemas.get(t) {
parent.as_ref().compile(db, t, t.clone(), errors);
if let Some(p_props) = parent.obj.compiled_properties.get() {
props.extend(p_props.clone());
}
}
}
}
if let Some(crate::database::object::SchemaTypeOrArray::Multiple(types)) = &self.obj.type_ {
let mut custom_type_count = 0;
for t in types {
if !crate::database::object::is_primitive_type(t) {
custom_type_count += 1;
}
}
if custom_type_count > 1 {
errors.push(crate::drop::Error {
code: "MULTIPLE_INHERITANCE_PROHIBITED".to_string(),
message: format!(
"Schema attempts to extend multiple custom object pointers in its type array {:?}. Use 'oneOf' for polymorphism and tagged unions.",
types
),
details: crate::drop::ErrorDetails {
path: Some(path.clone()),
schema: Some(root_id.to_string()),
..Default::default()
}
});
}
for t in types {
if !crate::database::object::is_primitive_type(t) {
if let Some(parent) = db.schemas.get(t) {
parent.as_ref().compile(db, t, t.clone(), errors);
}
}
}
}
// 2. Add local properties
if let Some(local_props) = &self.obj.properties {
for (k, v) in local_props {
props.insert(k.clone(), v.clone());
}
}
// 3. Add cases conditionally-defined properties recursively
if let Some(cases) = &self.obj.cases {
for (i, c) in cases.iter().enumerate() {
if let Some(child) = &c.when {
child.compile(db, root_id, format!("{}/cases/{}/when", path, i), errors);
}
if let Some(child) = &c.then {
child.compile(db, root_id, format!("{}/cases/{}/then", path, i), errors);
if let Some(t_props) = child.obj.compiled_properties.get() {
props.extend(t_props.clone());
}
}
if let Some(child) = &c.else_ {
child.compile(db, root_id, format!("{}/cases/{}/else", path, i), errors);
if let Some(e_props) = child.obj.compiled_properties.get() {
props.extend(e_props.clone());
}
}
}
}
// 4. Set the OnceLock!
let _ = self.obj.compiled_properties.set(props.clone());
let mut names: Vec<String> = props.keys().cloned().collect();
names.sort();
let _ = self.obj.compiled_property_names.set(names);
// 5. Compute Edges natively
let schema_edges = self.compile_edges(db, root_id, &path, &props, errors);
let _ = self.obj.compiled_edges.set(schema_edges);
// 5. Build our inline children properties recursively NOW! (Depth-first search)
if let Some(local_props) = &self.obj.properties {
for (k, child) in local_props {
child.compile(db, root_id, format!("{}/{}", path, k), errors);
}
}
if let Some(items) = &self.obj.items {
items.compile(db, root_id, format!("{}/items", path), errors);
}
if let Some(pattern_props) = &self.obj.pattern_properties {
for (k, child) in pattern_props {
child.compile(db, root_id, format!("{}/{}", path, k), errors);
}
}
if let Some(additional_props) = &self.obj.additional_properties {
additional_props.compile(
db,
root_id,
format!("{}/additionalProperties", path),
errors,
);
}
if let Some(one_of) = &self.obj.one_of {
for (i, child) in one_of.iter().enumerate() {
child.compile(db, root_id, format!("{}/oneOf/{}", path, i), errors);
}
}
if let Some(arr) = &self.obj.prefix_items {
for (i, child) in arr.iter().enumerate() {
child.compile(db, root_id, format!("{}/prefixItems/{}", path, i), errors);
}
}
if let Some(child) = &self.obj.not {
child.compile(db, root_id, format!("{}/not", path), errors);
}
if let Some(child) = &self.obj.contains {
child.compile(db, root_id, format!("{}/contains", path), errors);
}
self.compile_polymorphism(db, root_id, &path, errors);
}
}

View File

@ -0,0 +1,153 @@
use crate::database::schema::Schema;
impl Schema {
pub fn compile_polymorphism(
&self,
db: &crate::database::Database,
root_id: &str,
path: &str,
errors: &mut Vec<crate::drop::Error>,
) {
let mut options = std::collections::BTreeMap::new();
let mut strategy = String::new();
if let Some(family) = &self.obj.family {
let family_base = family.split('.').next_back().unwrap_or(family).to_string();
let family_prefix = family
.strip_suffix(&family_base)
.unwrap_or("")
.trim_end_matches('.');
if let Some(type_def) = db.types.get(&family_base) {
if type_def.variations.len() > 1 && type_def.variations.iter().any(|v| v != &family_base) {
// Scenario A / B: Table Variations
strategy = "type".to_string();
for var in &type_def.variations {
let target_id = if family_prefix.is_empty() {
var.to_string()
} else {
format!("{}.{}", family_prefix, var)
};
if db.schemas.contains_key(&target_id) {
options.insert(var.to_string(), (None, Some(target_id)));
}
}
} else {
// Scenario C: Single Table Inheritance (Horizontal)
strategy = "kind".to_string();
let suffix = format!(".{}", family_base);
for (id, schema) in &type_def.schemas {
if id.ends_with(&suffix) || id == &family_base {
if let Some(kind_val) = schema.obj.get_discriminator_value("kind", id) {
options.insert(kind_val, (None, Some(id.to_string())));
}
}
}
}
}
} else if let Some(one_of) = &self.obj.one_of {
let mut type_vals = std::collections::HashSet::new();
let mut kind_vals = std::collections::HashSet::new();
let mut disjoint_base = true;
let mut structural_types = std::collections::HashSet::new();
for c in one_of {
let mut child_id = String::new();
let mut child_is_primitive = false;
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) = &c.obj.type_ {
if crate::database::object::is_primitive_type(t) {
child_is_primitive = true;
structural_types.insert(t.clone());
} else {
child_id = t.clone();
structural_types.insert("object".to_string());
}
} else {
disjoint_base = false;
}
if !child_is_primitive {
if let Some(t_val) = c.obj.get_discriminator_value("type", &child_id) {
type_vals.insert(t_val);
}
if let Some(k_val) = c.obj.get_discriminator_value("kind", &child_id) {
kind_vals.insert(k_val);
}
}
}
if disjoint_base && structural_types.len() == one_of.len() {
strategy = "".to_string();
for (i, c) in one_of.iter().enumerate() {
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) = &c.obj.type_ {
if crate::database::object::is_primitive_type(t) {
options.insert(t.clone(), (Some(i), None));
} else {
options.insert("object".to_string(), (Some(i), None));
}
}
}
} else {
strategy = if type_vals.len() > 1 && type_vals.len() == one_of.len() {
"type".to_string()
} else if kind_vals.len() > 1 && kind_vals.len() == one_of.len() {
"kind".to_string()
} else {
"".to_string()
};
if strategy.is_empty() {
errors.push(crate::drop::Error {
code: "AMBIGUOUS_POLYMORPHISM".to_string(),
message: format!("oneOf boundaries must map mathematically unique 'type' or 'kind' discriminators, or strictly contain disjoint primitive types."),
details: crate::drop::ErrorDetails {
path: Some(path.to_string()),
schema: Some(root_id.to_string()),
..Default::default()
}
});
return;
}
for (i, c) in one_of.iter().enumerate() {
let mut child_id = String::new();
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) = &c.obj.type_ {
if !crate::database::object::is_primitive_type(t) {
child_id = t.clone();
}
}
if let Some(val) = c.obj.get_discriminator_value(&strategy, &child_id) {
if options.contains_key(&val) {
errors.push(crate::drop::Error {
code: "POLYMORPHIC_COLLISION".to_string(),
message: format!("Polymorphic boundary defines multiple candidates mapped to the identical discriminator value '{}'.", val),
details: crate::drop::ErrorDetails {
path: Some(path.to_string()),
schema: Some(root_id.to_string()),
..Default::default()
}
});
continue;
}
options.insert(val, (Some(i), None));
}
}
}
} else {
return;
}
if !options.is_empty() {
if !strategy.is_empty() {
let _ = self.obj.compiled_discriminator.set(strategy);
}
let _ = self.obj.compiled_options.set(options);
}
}
}

View File

@ -1,3 +1,4 @@
pub mod compile;
pub mod edge;
pub mod r#enum;
pub mod executors;
@ -209,6 +210,7 @@ impl Database {
}
pub fn compile(&mut self, errors: &mut Vec<crate::drop::Error>) {
// Collect existing schemas patched in the databse
let mut harvested = Vec::new();
for (id, schema_arc) in &self.schemas {
crate::database::schema::Schema::collect_schemas(
@ -233,6 +235,37 @@ impl Database {
.as_ref()
.compile(self, root_id, id.clone(), errors);
}
// Phase 2: Synthesize Composed Filter References
let mut filter_schemas = Vec::new();
for type_def in self.types.values() {
for (id, schema_arc) in &type_def.schemas {
// Only run synthesis on actual structured, table-backed boundaries. Exclude subschemas!
let base_name = id.split('.').last().unwrap_or(id);
let is_table_backed = base_name == type_def.name;
if is_table_backed && !id.contains('/') {
if let Some(filter_schema) = schema_arc.compile_filter(self, id, errors) {
filter_schemas.push((format!("{}.filter", id), Arc::new(filter_schema)));
}
}
}
}
let mut filter_ids = Vec::new();
for (id, filter_arc) in filter_schemas {
filter_ids.push(id.clone());
self.schemas.insert(id, filter_arc);
}
// Now actively compile the newly injected filters to lock all nested compose references natively
for id in filter_ids {
if let Some(filter_arc) = self.schemas.get(&id).cloned() {
let root_id = id.split('/').next().unwrap_or(&id);
filter_arc
.as_ref()
.compile(self, root_id, id.clone(), errors);
}
}
}
fn collect_schemas(&mut self, errors: &mut Vec<crate::drop::Error>) {

View File

@ -1,7 +1,7 @@
use crate::database::object::*;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::sync::Arc;
#[derive(Debug, Clone, Serialize, Default)]
pub struct Schema {
#[serde(flatten)]
@ -22,609 +22,6 @@ impl std::ops::DerefMut for Schema {
}
}
impl Schema {
pub fn compile(
&self,
db: &crate::database::Database,
root_id: &str,
path: String,
errors: &mut Vec<crate::drop::Error>,
) {
if self.obj.compiled_properties.get().is_some() {
return;
}
if let Some(format_str) = &self.obj.format {
if let Some(fmt) = crate::database::formats::FORMATS.get(format_str.as_str()) {
let _ = self
.obj
.compiled_format
.set(crate::database::object::CompiledFormat::Func(fmt.func));
}
}
if let Some(pattern_str) = &self.obj.pattern {
if let Ok(re) = regex::Regex::new(pattern_str) {
let _ = self
.obj
.compiled_pattern
.set(crate::database::object::CompiledRegex(re));
}
}
if let Some(pattern_props) = &self.obj.pattern_properties {
let mut compiled = Vec::new();
for (k, v) in pattern_props {
if let Ok(re) = regex::Regex::new(k) {
compiled.push((crate::database::object::CompiledRegex(re), v.clone()));
}
}
if !compiled.is_empty() {
let _ = self.obj.compiled_pattern_properties.set(compiled);
}
}
let mut props = std::collections::BTreeMap::new();
// 1. Resolve INHERITANCE dependencies first
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) = &self.obj.type_ {
if !crate::database::object::is_primitive_type(t) {
if let Some(parent) = db.schemas.get(t) {
parent.as_ref().compile(db, t, t.clone(), errors);
if let Some(p_props) = parent.obj.compiled_properties.get() {
props.extend(p_props.clone());
}
}
}
}
if let Some(crate::database::object::SchemaTypeOrArray::Multiple(types)) = &self.obj.type_ {
let mut custom_type_count = 0;
for t in types {
if !crate::database::object::is_primitive_type(t) {
custom_type_count += 1;
}
}
if custom_type_count > 1 {
errors.push(crate::drop::Error {
code: "MULTIPLE_INHERITANCE_PROHIBITED".to_string(),
message: format!(
"Schema attempts to extend multiple custom object pointers in its type array {:?}. Use 'oneOf' for polymorphism and tagged unions.",
types
),
details: crate::drop::ErrorDetails {
path: Some(path.clone()),
schema: Some(root_id.to_string()),
..Default::default()
}
});
}
for t in types {
if !crate::database::object::is_primitive_type(t) {
if let Some(parent) = db.schemas.get(t) {
parent.as_ref().compile(db, t, t.clone(), errors);
}
}
}
}
// 2. Add local properties
if let Some(local_props) = &self.obj.properties {
for (k, v) in local_props {
props.insert(k.clone(), v.clone());
}
}
// 3. Add cases conditionally-defined properties recursively
if let Some(cases) = &self.obj.cases {
for (i, c) in cases.iter().enumerate() {
if let Some(child) = &c.when {
child.compile(db, root_id, format!("{}/cases/{}/when", path, i), errors);
}
if let Some(child) = &c.then {
child.compile(db, root_id, format!("{}/cases/{}/then", path, i), errors);
if let Some(t_props) = child.obj.compiled_properties.get() {
props.extend(t_props.clone());
}
}
if let Some(child) = &c.else_ {
child.compile(db, root_id, format!("{}/cases/{}/else", path, i), errors);
if let Some(e_props) = child.obj.compiled_properties.get() {
props.extend(e_props.clone());
}
}
}
}
// 4. Set the OnceLock!
let _ = self.obj.compiled_properties.set(props.clone());
let mut names: Vec<String> = props.keys().cloned().collect();
names.sort();
let _ = self.obj.compiled_property_names.set(names);
// 5. Compute Edges natively
let schema_edges = self.compile_edges(db, root_id, &path, &props, errors);
let _ = self.obj.compiled_edges.set(schema_edges);
// 5. Build our inline children properties recursively NOW! (Depth-first search)
if let Some(local_props) = &self.obj.properties {
for (k, child) in local_props {
child.compile(db, root_id, format!("{}/{}", path, k), errors);
}
}
if let Some(items) = &self.obj.items {
items.compile(db, root_id, format!("{}/items", path), errors);
}
if let Some(pattern_props) = &self.obj.pattern_properties {
for (k, child) in pattern_props {
child.compile(db, root_id, format!("{}/{}", path, k), errors);
}
}
if let Some(additional_props) = &self.obj.additional_properties {
additional_props.compile(
db,
root_id,
format!("{}/additionalProperties", path),
errors,
);
}
if let Some(one_of) = &self.obj.one_of {
for (i, child) in one_of.iter().enumerate() {
child.compile(db, root_id, format!("{}/oneOf/{}", path, i), errors);
}
}
if let Some(arr) = &self.obj.prefix_items {
for (i, child) in arr.iter().enumerate() {
child.compile(db, root_id, format!("{}/prefixItems/{}", path, i), errors);
}
}
if let Some(child) = &self.obj.not {
child.compile(db, root_id, format!("{}/not", path), errors);
}
if let Some(child) = &self.obj.contains {
child.compile(db, root_id, format!("{}/contains", path), errors);
}
self.compile_polymorphism(db, root_id, &path, errors);
}
/// Dynamically infers and compiles all structural database relationships between this Schema
/// and its nested children. This functions recursively traverses the JSON Schema abstract syntax
/// tree, identifies physical PostgreSQL table boundaries, and locks the resulting relation
/// constraint paths directly onto the `compiled_edges` map in O(1) memory.
pub fn compile_edges(
&self,
db: &crate::database::Database,
root_id: &str,
path: &str,
props: &std::collections::BTreeMap<String, std::sync::Arc<Schema>>,
errors: &mut Vec<crate::drop::Error>,
) -> std::collections::BTreeMap<String, crate::database::edge::Edge> {
let mut schema_edges = std::collections::BTreeMap::new();
// Determine the physical Database Table Name this schema structurally represents
// Plucks the polymorphic discriminator via dot-notation (e.g. extracting "person" from "full.person")
let mut parent_type_name = None;
if let Some(family) = &self.obj.family {
// 1. Explicit horizontal routing
parent_type_name = Some(family.split('.').next_back().unwrap_or(family).to_string());
} else if path == root_id {
// 2. Root nodes trust their exact registry footprint
let base_type_name = path.split('.').next_back().unwrap_or(path).to_string();
if db.types.contains_key(&base_type_name) {
parent_type_name = Some(base_type_name);
}
} else if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) = &self.obj.type_ {
// 3. Nested graphs trust their explicit struct pointer reference
if !crate::database::object::is_primitive_type(t) {
parent_type_name = Some(t.split('.').next_back().unwrap_or(t).to_string());
}
}
if let Some(p_type) = parent_type_name {
// Proceed only if the resolved table physically exists within the Postgres Type hierarchy
if let Some(type_def) = db.types.get(&p_type) {
// Iterate over all discovered schema boundaries mapped inside the object
for (prop_name, prop_schema) in props {
let mut child_type_name = None;
let mut target_schema = prop_schema.clone();
let mut is_array = false;
// Structurally unpack the inner target entity if the object maps to an array list
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) =
&prop_schema.obj.type_
{
if t == "array" {
is_array = true;
if let Some(items) = &prop_schema.obj.items {
target_schema = items.clone();
}
}
}
// Determine the physical Postgres table backing the nested child schema recursively
if let Some(family) = &target_schema.obj.family {
child_type_name = Some(family.split('.').next_back().unwrap_or(family).to_string());
} else if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) =
&target_schema.obj.type_
{
if !crate::database::object::is_primitive_type(t) {
child_type_name = Some(t.split('.').next_back().unwrap_or(t).to_string());
}
} else if let Some(arr) = &target_schema.obj.one_of {
if let Some(first) = arr.first() {
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) = &first.obj.type_
{
if !crate::database::object::is_primitive_type(t) {
child_type_name = Some(t.split('.').next_back().unwrap_or(t).to_string());
}
}
}
}
if let Some(c_type) = child_type_name {
// Skip edge compilation for JSONB columns — they store data inline, not relationally.
// The physical column type from field_types is the single source of truth.
if let Some(ft) = type_def
.field_types
.as_ref()
.and_then(|v| v.get(prop_name.as_str()))
.and_then(|v| v.as_str())
{
if ft == "jsonb" {
continue;
}
}
if db.types.contains_key(&c_type) {
// Ensure the child Schema's AST has accurately compiled its own physical property keys so we can
// inject them securely for Many-to-Many Twin Deduction disambiguation matching.
target_schema.compile(db, root_id, format!("{}/{}", path, prop_name), errors);
if let Some(compiled_target_props) = target_schema.obj.compiled_properties.get() {
let keys_for_ambiguity: Vec<String> =
compiled_target_props.keys().cloned().collect();
// Interrogate the Database catalog graph to discover the exact Foreign Key Constraint connecting the components
if let Some((relation, is_forward)) = db.resolve_relation(
&p_type,
&c_type,
prop_name,
Some(&keys_for_ambiguity),
is_array,
Some(root_id),
&format!("{}/{}", path, prop_name),
errors,
) {
schema_edges.insert(
prop_name.clone(),
crate::database::edge::Edge {
constraint: relation.constraint.clone(),
forward: is_forward,
},
);
}
}
}
}
}
}
}
schema_edges
}
pub fn compile_polymorphism(
&self,
db: &crate::database::Database,
root_id: &str,
path: &str,
errors: &mut Vec<crate::drop::Error>,
) {
let mut options = std::collections::BTreeMap::new();
let mut strategy = String::new();
if let Some(family) = &self.obj.family {
let family_base = family.split('.').next_back().unwrap_or(family).to_string();
let family_prefix = family
.strip_suffix(&family_base)
.unwrap_or("")
.trim_end_matches('.');
if let Some(type_def) = db.types.get(&family_base) {
if type_def.variations.len() > 1 && type_def.variations.iter().any(|v| v != &family_base) {
// Scenario A / B: Table Variations
strategy = "type".to_string();
for var in &type_def.variations {
let target_id = if family_prefix.is_empty() {
var.to_string()
} else {
format!("{}.{}", family_prefix, var)
};
if db.schemas.contains_key(&target_id) {
options.insert(var.to_string(), (None, Some(target_id)));
}
}
} else {
// Scenario C: Single Table Inheritance (Horizontal)
strategy = "kind".to_string();
let suffix = format!(".{}", family_base);
for (id, schema) in &type_def.schemas {
if id.ends_with(&suffix) || id == &family_base {
if let Some(kind_val) = schema.obj.get_discriminator_value("kind", id) {
options.insert(kind_val, (None, Some(id.to_string())));
}
}
}
}
}
} else if let Some(one_of) = &self.obj.one_of {
let mut type_vals = std::collections::HashSet::new();
let mut kind_vals = std::collections::HashSet::new();
let mut disjoint_base = true;
let mut structural_types = std::collections::HashSet::new();
for c in one_of {
let mut child_id = String::new();
let mut child_is_primitive = false;
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) = &c.obj.type_ {
if crate::database::object::is_primitive_type(t) {
child_is_primitive = true;
structural_types.insert(t.clone());
} else {
child_id = t.clone();
structural_types.insert("object".to_string());
}
} else {
disjoint_base = false;
}
if !child_is_primitive {
if let Some(t_val) = c.obj.get_discriminator_value("type", &child_id) {
type_vals.insert(t_val);
}
if let Some(k_val) = c.obj.get_discriminator_value("kind", &child_id) {
kind_vals.insert(k_val);
}
}
}
if disjoint_base && structural_types.len() == one_of.len() {
strategy = "".to_string();
for (i, c) in one_of.iter().enumerate() {
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) = &c.obj.type_ {
if crate::database::object::is_primitive_type(t) {
options.insert(t.clone(), (Some(i), None));
} else {
options.insert("object".to_string(), (Some(i), None));
}
}
}
} else {
strategy = if type_vals.len() > 1 && type_vals.len() == one_of.len() {
"type".to_string()
} else if kind_vals.len() > 1 && kind_vals.len() == one_of.len() {
"kind".to_string()
} else {
"".to_string()
};
if strategy.is_empty() {
errors.push(crate::drop::Error {
code: "AMBIGUOUS_POLYMORPHISM".to_string(),
message: format!("oneOf boundaries must map mathematically unique 'type' or 'kind' discriminators, or strictly contain disjoint primitive types."),
details: crate::drop::ErrorDetails {
path: Some(path.to_string()),
schema: Some(root_id.to_string()),
..Default::default()
}
});
return;
}
for (i, c) in one_of.iter().enumerate() {
let mut child_id = String::new();
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) = &c.obj.type_ {
if !crate::database::object::is_primitive_type(t) {
child_id = t.clone();
}
}
if let Some(val) = c.obj.get_discriminator_value(&strategy, &child_id) {
if options.contains_key(&val) {
errors.push(crate::drop::Error {
code: "POLYMORPHIC_COLLISION".to_string(),
message: format!("Polymorphic boundary defines multiple candidates mapped to the identical discriminator value '{}'.", val),
details: crate::drop::ErrorDetails {
path: Some(path.to_string()),
schema: Some(root_id.to_string()),
..Default::default()
}
});
continue;
}
options.insert(val, (Some(i), None));
}
}
}
} else {
return;
}
if !options.is_empty() {
if !strategy.is_empty() {
let _ = self.obj.compiled_discriminator.set(strategy);
}
let _ = self.obj.compiled_options.set(options);
}
}
#[allow(unused_variables)]
fn validate_identifier(
id: &str,
field_name: &str,
root_id: &str,
path: &str,
errors: &mut Vec<crate::drop::Error>,
) {
#[cfg(not(test))]
for c in id.chars() {
if !c.is_ascii_lowercase() && !c.is_ascii_digit() && c != '_' && c != '.' {
errors.push(crate::drop::Error {
code: "INVALID_IDENTIFIER".to_string(),
message: format!(
"Invalid character '{}' in JSON Schema '{}' property: '{}'. Identifiers must exclusively contain [a-z0-9_.]",
c, field_name, id
),
details: crate::drop::ErrorDetails {
path: Some(path.to_string()),
schema: Some(root_id.to_string()),
..Default::default()
},
});
return;
}
}
}
pub fn collect_schemas(
schema_arc: &Arc<Schema>,
root_id: &str,
path: String,
to_insert: &mut Vec<(String, Arc<Schema>)>,
errors: &mut Vec<crate::drop::Error>,
) {
if let Some(crate::database::object::SchemaTypeOrArray::Single(t)) = &schema_arc.obj.type_ {
if t == "array" {
if let Some(items) = &schema_arc.obj.items {
if let Some(crate::database::object::SchemaTypeOrArray::Single(it)) = &items.obj.type_ {
if !crate::database::object::is_primitive_type(it) {
if items.obj.properties.is_some() || items.obj.cases.is_some() {
to_insert.push((path.clone(), Arc::clone(schema_arc)));
}
}
}
}
} else if !crate::database::object::is_primitive_type(t) {
Self::validate_identifier(t, "type", root_id, &path, errors);
// Is this an explicit inline ad-hoc composition?
if schema_arc.obj.properties.is_some() || schema_arc.obj.cases.is_some() {
to_insert.push((path.clone(), Arc::clone(schema_arc)));
}
}
}
if let Some(family) = &schema_arc.obj.family {
Self::validate_identifier(family, "family", root_id, &path, errors);
}
Self::collect_child_schemas(schema_arc, root_id, path, to_insert, errors);
}
pub fn collect_child_schemas(
schema_arc: &Arc<Schema>,
root_id: &str,
path: String,
to_insert: &mut Vec<(String, Arc<Schema>)>,
errors: &mut Vec<crate::drop::Error>,
) {
if let Some(props) = &schema_arc.obj.properties {
for (k, v) in props.iter() {
let next_path = format!("{}/{}", path, k);
Self::collect_schemas(v, root_id, next_path, to_insert, errors);
}
}
if let Some(pattern_props) = &schema_arc.obj.pattern_properties {
for (k, v) in pattern_props.iter() {
let next_path = format!("{}/{}", path, k);
Self::collect_schemas(v, root_id, next_path, to_insert, errors);
}
}
let mut map_arr = |arr: &Vec<Arc<Schema>>, sub: &str| {
for (i, v) in arr.iter().enumerate() {
Self::collect_schemas(
v,
root_id,
format!("{}/{}/{}", path, sub, i),
to_insert,
errors,
);
}
};
if let Some(arr) = &schema_arc.obj.prefix_items {
map_arr(arr, "prefixItems");
}
if let Some(arr) = &schema_arc.obj.one_of {
map_arr(arr, "oneOf");
}
let mut map_opt = |opt: &Option<Arc<Schema>>, pass_path: bool, sub: &str| {
if let Some(v) = opt {
if pass_path {
// Arrays explicitly push their wrapper natively.
// 'items' becomes a transparent conduit, bypassing self-promotion and skipping the '/items' suffix.
Self::collect_child_schemas(v, root_id, path.clone(), to_insert, errors);
} else {
Self::collect_child_schemas(v, root_id, format!("{}/{}", path, sub), to_insert, errors);
}
}
};
map_opt(
&schema_arc.obj.additional_properties,
false,
"additionalProperties",
);
map_opt(&schema_arc.obj.items, true, "items");
map_opt(&schema_arc.obj.not, false, "not");
map_opt(&schema_arc.obj.contains, false, "contains");
map_opt(&schema_arc.obj.property_names, false, "propertyNames");
if let Some(cases) = &schema_arc.obj.cases {
for (i, c) in cases.iter().enumerate() {
if let Some(when) = &c.when {
Self::collect_schemas(
when,
root_id,
format!("{}/cases/{}/when", path, i),
to_insert,
errors,
);
}
if let Some(then) = &c.then {
Self::collect_schemas(
then,
root_id,
format!("{}/cases/{}/then", path, i),
to_insert,
errors,
);
}
if let Some(else_) = &c.else_ {
Self::collect_schemas(
else_,
root_id,
format!("{}/cases/{}/else", path, i),
to_insert,
errors,
);
}
}
}
}
}
impl<'de> Deserialize<'de> for Schema {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where

View File

@ -717,8 +717,8 @@ impl<'a> Compiler<'a> {
let param_index = i + 1;
let p_val = format!("${}#>>'{{}}'", param_index);
if op == "$in" || op == "$nin" {
let sql_op = if op == "$in" { "IN" } else { "NOT IN" };
if op == "$of" || op == "$nof" {
let sql_op = if op == "$of" { "IN" } else { "NOT IN" };
let subquery = format!(
"(SELECT value{} FROM jsonb_array_elements_text(({})::jsonb))",
cast, p_val

View File

@ -533,6 +533,12 @@ fn test_unique_items_6_1() {
crate::tests::runner::run_test_case(&path, 6, 1).unwrap();
}
#[test]
fn test_filter_0_0() {
let path = format!("{}/fixtures/filter.json", env!("CARGO_MANIFEST_DIR"));
crate::tests::runner::run_test_case(&path, 0, 0).unwrap();
}
#[test]
fn test_min_items_0_0() {
let path = format!("{}/fixtures/minItems.json", env!("CARGO_MANIFEST_DIR"));

View File

@ -107,12 +107,28 @@ fn test_library_api() {
}
}
},
"source_schema.filter": {
"type": "object",
"properties": {
"type": { "type": ["string.condition", "null"] },
"name": { "type": ["string.condition", "null"] },
"target": { "type": ["target_schema.filter", "null"] }
},
"compiledPropertyNames": ["name", "target", "type"]
},
"target_schema": {
"type": "object",
"properties": {
"value": { "type": "number" }
},
"compiledPropertyNames": ["value"]
},
"target_schema.filter": {
"type": "object",
"properties": {
"value": { "type": ["number.condition", "null"] }
},
"compiledPropertyNames": ["value"]
}
}
})

View File

@ -20,5 +20,5 @@ pub struct Expect {
#[serde(default)]
pub sql: Option<Vec<SqlExpectation>>,
#[serde(default)]
pub schemas: Option<Vec<String>>,
pub schemas: Option<std::collections::HashMap<String, serde_json::Value>>,
}

View File

@ -3,13 +3,13 @@ use std::sync::Arc;
impl Expect {
pub fn assert_schemas(&self, db: &Arc<crate::database::Database>) -> Result<(), String> {
if let Some(expected_schemas) = &self.schemas {
if let Some(expected_map) = &self.schemas {
// Collect actual schemas and sort
let mut actual: Vec<String> = db.schemas.keys().cloned().collect();
actual.sort();
// Collect expected schemas and sort
let mut expected: Vec<String> = expected_schemas.clone();
let mut expected: Vec<String> = expected_map.keys().cloned().collect();
expected.sort();
if actual != expected {
@ -21,6 +21,23 @@ impl Expect {
actual
));
}
for (key, expected_val) in expected_map {
if expected_val.is_object() && expected_val.as_object().unwrap().is_empty() {
continue; // A `{}` means we just wanted to test it was collected/promoted, skip deep match
}
let actual_ast = db.schemas.get(key).unwrap();
let actual_val = serde_json::to_value(actual_ast).unwrap();
if actual_val != *expected_val {
return Err(format!(
"Detailed Schema Match Failure for '{}'!\n\nExpected:\n{}\n\nActual:\n{}",
key,
serde_json::to_string_pretty(expected_val).unwrap(),
serde_json::to_string_pretty(&actual_val).unwrap()
));
}
}
}
Ok(())
}

View File

@ -1 +1 @@
1.0.122
1.0.123