Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5e55786e3e | |||
| 6520413069 | |||
| b97879ff61 | |||
| ea0b139f87 |
49
Cargo.lock
generated
49
Cargo.lock
generated
@ -362,6 +362,15 @@ version = "0.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6"
|
||||
|
||||
[[package]]
|
||||
name = "codepage"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "48f68d061bc2828ae826206326e61251aca94c1e4a5305cf52d9138639c918b4"
|
||||
dependencies = [
|
||||
"encoding_rs",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "convert_case"
|
||||
version = "0.8.0"
|
||||
@ -418,6 +427,15 @@ version = "1.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
|
||||
|
||||
[[package]]
|
||||
name = "encoding_rs"
|
||||
version = "0.8.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "75030f3c4f45dafd7586dd6780965a8c7e8e285a5ecb86713e63a79c5b2766f3"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "enum-map"
|
||||
version = "2.7.3"
|
||||
@ -1106,9 +1124,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "pgrx"
|
||||
version = "0.14.0"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3e1b41219b12cfcaa5d58f946a7ff1e7ddf0a4f7f930a7cdab612916e8a12c64"
|
||||
checksum = "bab5bc1d60d3bc3c966d307a3c7313b1ebfb49a0ec183be3f1a057df0bcc9988"
|
||||
dependencies = [
|
||||
"atomic-traits",
|
||||
"bitflags",
|
||||
@ -1130,9 +1148,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "pgrx-bindgen"
|
||||
version = "0.14.0"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6afcef51e801bb18662716f1c524cedfb7943844593171734fe4d3a94c9afa12"
|
||||
checksum = "9804b74c211a9edd550cd974718f8cc407dec50d8e9cafb906e0b042ba434af0"
|
||||
dependencies = [
|
||||
"bindgen",
|
||||
"cc",
|
||||
@ -1149,9 +1167,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "pgrx-macros"
|
||||
version = "0.14.0"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "729af3e6954d2f76230d700efd8606121f13f71f800e5c76173add2c02097948"
|
||||
checksum = "f230769493bf567f137de23264d604d267dd72b8a77c596528e43cf423c6208e"
|
||||
dependencies = [
|
||||
"pgrx-sql-entity-graph",
|
||||
"proc-macro2",
|
||||
@ -1161,11 +1179,13 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "pgrx-pg-config"
|
||||
version = "0.14.0"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "116e33a329f3fac976b5f3150f14f2612735dfc56a15cb0a0800f25a3bd90aa7"
|
||||
checksum = "49b64c071c2a46a19ab4521120a25b02b598f4abf6e9b4b1769a7922edeee3de"
|
||||
dependencies = [
|
||||
"cargo_toml",
|
||||
"codepage",
|
||||
"encoding_rs",
|
||||
"eyre",
|
||||
"home",
|
||||
"owo-colors",
|
||||
@ -1175,13 +1195,14 @@ dependencies = [
|
||||
"thiserror 2.0.12",
|
||||
"toml",
|
||||
"url",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pgrx-pg-sys"
|
||||
version = "0.14.0"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cd074044513f1f7fc63fd1ed0117ad0fbe690ef1b445f6d72b92e611b3846490"
|
||||
checksum = "fcbfa98ec7a90252d13a78ac666541173dbb01a2fc1ba20131db6490c0711125"
|
||||
dependencies = [
|
||||
"cee-scape",
|
||||
"libc",
|
||||
@ -1194,9 +1215,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "pgrx-sql-entity-graph"
|
||||
version = "0.14.0"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0eb73c4b916d4abb422fff66c2606c46bf4b99136209306836e89766a8d49cd"
|
||||
checksum = "e79bbf5a33cff6cfdc6dda3a976cd931c995eaa2c073a7c59b8f8fe8f6faa073"
|
||||
dependencies = [
|
||||
"convert_case",
|
||||
"eyre",
|
||||
@ -1210,9 +1231,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "pgrx-tests"
|
||||
version = "0.14.0"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aba4e9a97fd148c9f65cf0c56d33a4cde4deb9941f5c0d914a39148e8148a7a6"
|
||||
checksum = "9791c709882f3af9545bcca71670fdd82768f67a428b416b6210eae3773dbd0d"
|
||||
dependencies = [
|
||||
"clap-cargo",
|
||||
"eyre",
|
||||
|
||||
@ -1,17 +1,17 @@
|
||||
[package]
|
||||
name = "jspg"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
pgrx = "0.14.0"
|
||||
pgrx = "0.15.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
lazy_static = "1.5.0"
|
||||
boon = "0.6.1"
|
||||
|
||||
[dev-dependencies]
|
||||
pgrx-tests = "0.14.0"
|
||||
pgrx-tests = "0.15.0"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib", "lib"]
|
||||
|
||||
6
flow
6
flow
@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Flows
|
||||
source ./flows/base
|
||||
@ -11,7 +11,7 @@ source ./flows/rust
|
||||
POSTGRES_VERSION="17"
|
||||
POSTGRES_CONFIG_PATH="/opt/homebrew/opt/postgresql@${POSTGRES_VERSION}/bin/pg_config"
|
||||
DEPENDENCIES+=(icu4c pkg-config "postgresql@${POSTGRES_VERSION}")
|
||||
CARGO_DEPENDENCIES=(cargo-pgrx==0.14.0)
|
||||
CARGO_DEPENDENCIES=(cargo-pgrx==0.15.0)
|
||||
GITEA_ORGANIZATION="cellular"
|
||||
GITEA_REPOSITORY="jspg"
|
||||
|
||||
@ -129,4 +129,4 @@ jspg-flow() {
|
||||
|
||||
register-flow "jspg-usage" "jspg-flow"
|
||||
|
||||
dispatch "$@"
|
||||
dispatch "$@"
|
||||
254
src/lib.rs
254
src/lib.rs
@ -31,65 +31,225 @@ lazy_static! {
|
||||
}
|
||||
|
||||
#[pg_extern(strict)]
|
||||
fn cache_json_schema(schema_id: &str, schema: JsonB, strict: bool) -> JsonB {
|
||||
fn cache_json_schemas(types: JsonB, puncs: JsonB) -> JsonB {
|
||||
let mut cache = SCHEMA_CACHE.write().unwrap();
|
||||
let mut schema_value: Value = schema.0;
|
||||
let schema_path = format!("urn:{}", schema_id);
|
||||
let types_value: Value = types.0;
|
||||
let puncs_value: Value = puncs.0;
|
||||
|
||||
// Apply strict validation to all objects in the schema if requested
|
||||
if strict {
|
||||
apply_strict_validation(&mut schema_value);
|
||||
}
|
||||
// Clear existing cache
|
||||
*cache = BoonCache {
|
||||
schemas: Schemas::new(),
|
||||
id_to_index: HashMap::new(),
|
||||
};
|
||||
|
||||
// Create the boon compiler and enable format assertions
|
||||
let mut compiler = Compiler::new();
|
||||
compiler.enable_format_assertions();
|
||||
|
||||
// Use schema_path when adding the resource
|
||||
if let Err(e) = compiler.add_resource(&schema_path, schema_value.clone()) {
|
||||
return JsonB(json!({
|
||||
"errors": [{
|
||||
"code": "SCHEMA_RESOURCE_ADD_FAILED",
|
||||
"message": format!("Failed to add schema resource '{}'", schema_id),
|
||||
"details": {
|
||||
"schema": schema_id,
|
||||
"cause": format!("{}", e)
|
||||
let mut errors = Vec::new();
|
||||
|
||||
// Track all schema IDs for compilation
|
||||
let mut all_schema_ids = Vec::new();
|
||||
|
||||
// Phase 1: Add all type schemas as resources (these are referenced by puncs)
|
||||
// Types are never strict - they're reusable building blocks
|
||||
if let Some(types_array) = types_value.as_array() {
|
||||
for type_row in types_array {
|
||||
if let Some(type_obj) = type_row.as_object() {
|
||||
if let (Some(type_name), Some(schemas_raw)) = (
|
||||
type_obj.get("name").and_then(|v| v.as_str()),
|
||||
type_obj.get("schemas")
|
||||
) {
|
||||
// Parse the schemas JSONB field
|
||||
if let Some(schemas_array) = schemas_raw.as_array() {
|
||||
for schema_def in schemas_array {
|
||||
if let Some(schema_id) = schema_def.get("$id").and_then(|v| v.as_str()) {
|
||||
if let Err(e) = add_schema_resource(&mut compiler, schema_id, schema_def.clone(), false, &mut errors) {
|
||||
errors.push(json!({
|
||||
"code": "TYPE_SCHEMA_RESOURCE_FAILED",
|
||||
"message": format!("Failed to add schema resource '{}' for type '{}'", schema_id, type_name),
|
||||
"details": {
|
||||
"type_name": type_name,
|
||||
"schema_id": schema_id,
|
||||
"cause": format!("{}", e)
|
||||
}
|
||||
}));
|
||||
} else {
|
||||
all_schema_ids.push(schema_id.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}]
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Use schema_path when compiling
|
||||
match compiler.compile(&schema_path, &mut cache.schemas) {
|
||||
Ok(sch_index) => {
|
||||
// Store the index using the original schema_id as the key
|
||||
cache.id_to_index.insert(schema_id.to_string(), sch_index);
|
||||
JsonB(json!({ "response": "success" }))
|
||||
}
|
||||
Err(e) => {
|
||||
let errors = match &e {
|
||||
CompileError::ValidationError { url: _url, src } => {
|
||||
// Collect leaf errors from the meta-schema validation failure
|
||||
let mut error_list = Vec::new();
|
||||
collect_errors(src, &mut error_list);
|
||||
// Filter and format errors properly - no instance for schema compilation
|
||||
format_errors(error_list, &schema_value, schema_id)
|
||||
}
|
||||
_ => {
|
||||
// Other compilation errors
|
||||
vec![json!({
|
||||
"code": "SCHEMA_COMPILATION_FAILED",
|
||||
"message": format!("Schema '{}' compilation failed", schema_id),
|
||||
"details": {
|
||||
"schema": schema_id,
|
||||
"cause": format!("{:?}", e)
|
||||
// Phase 2: Add all punc schemas as resources (these may reference type schemas)
|
||||
// Each punc gets strict validation based on its public field
|
||||
if let Some(puncs_array) = puncs_value.as_array() {
|
||||
for punc_row in puncs_array {
|
||||
if let Some(punc_obj) = punc_row.as_object() {
|
||||
if let Some(punc_name) = punc_obj.get("name").and_then(|v| v.as_str()) {
|
||||
// Get the strict setting for this specific punc (public = strict)
|
||||
let punc_strict = punc_obj.get("public")
|
||||
.and_then(|v| v.as_bool())
|
||||
.unwrap_or(false);
|
||||
|
||||
// Add punc local schemas as resources (from schemas field) - use $id directly (universal)
|
||||
if let Some(schemas_raw) = punc_obj.get("schemas") {
|
||||
if let Some(schemas_array) = schemas_raw.as_array() {
|
||||
for schema_def in schemas_array {
|
||||
if let Some(schema_id) = schema_def.get("$id").and_then(|v| v.as_str()) {
|
||||
if let Err(e) = add_schema_resource(&mut compiler, schema_id, schema_def.clone(), punc_strict, &mut errors) {
|
||||
errors.push(json!({
|
||||
"code": "PUNC_LOCAL_SCHEMA_RESOURCE_FAILED",
|
||||
"message": format!("Failed to add local schema resource '{}' for punc '{}'", schema_id, punc_name),
|
||||
"details": {
|
||||
"punc_name": punc_name,
|
||||
"schema_id": schema_id,
|
||||
"cause": format!("{}", e)
|
||||
}
|
||||
}));
|
||||
} else {
|
||||
all_schema_ids.push(schema_id.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})]
|
||||
}
|
||||
|
||||
// Add request schema as resource if present - use {punc_name}.request
|
||||
if let Some(request_schema) = punc_obj.get("request") {
|
||||
if !request_schema.is_null() {
|
||||
let request_schema_id = format!("{}.request", punc_name);
|
||||
if let Err(e) = add_schema_resource(&mut compiler, &request_schema_id, request_schema.clone(), punc_strict, &mut errors) {
|
||||
errors.push(json!({
|
||||
"code": "PUNC_REQUEST_SCHEMA_RESOURCE_FAILED",
|
||||
"message": format!("Failed to add request schema resource for punc '{}'", punc_name),
|
||||
"details": {
|
||||
"punc_name": punc_name,
|
||||
"schema_id": request_schema_id,
|
||||
"cause": format!("{}", e)
|
||||
}
|
||||
}));
|
||||
} else {
|
||||
all_schema_ids.push(request_schema_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add response schema as resource if present - use {punc_name}.response
|
||||
if let Some(response_schema) = punc_obj.get("response") {
|
||||
if !response_schema.is_null() {
|
||||
let response_schema_id = format!("{}.response", punc_name);
|
||||
if let Err(e) = add_schema_resource(&mut compiler, &response_schema_id, response_schema.clone(), punc_strict, &mut errors) {
|
||||
errors.push(json!({
|
||||
"code": "PUNC_RESPONSE_SCHEMA_RESOURCE_FAILED",
|
||||
"message": format!("Failed to add response schema resource for punc '{}'", punc_name),
|
||||
"details": {
|
||||
"punc_name": punc_name,
|
||||
"schema_id": response_schema_id,
|
||||
"cause": format!("{}", e)
|
||||
}
|
||||
}));
|
||||
} else {
|
||||
all_schema_ids.push(response_schema_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
JsonB(json!({ "errors": errors }))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Phase 3: Compile all schemas now that all resources are added
|
||||
if !errors.is_empty() {
|
||||
// If we had errors adding resources, don't attempt compilation
|
||||
return JsonB(json!({ "errors": errors }));
|
||||
}
|
||||
|
||||
if let Err(_) = compile_all_schemas(&mut compiler, &mut cache, &all_schema_ids, &mut errors) {
|
||||
// compile_all_schemas already adds errors to the errors vector
|
||||
}
|
||||
|
||||
if errors.is_empty() {
|
||||
JsonB(json!({ "response": "success" }))
|
||||
} else {
|
||||
JsonB(json!({ "errors": errors }))
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to add a schema resource (without compiling)
|
||||
fn add_schema_resource(
|
||||
compiler: &mut Compiler,
|
||||
schema_id: &str,
|
||||
mut schema_value: Value,
|
||||
strict: bool,
|
||||
errors: &mut Vec<Value>
|
||||
) -> Result<(), String> {
|
||||
// Apply strict validation to all objects in the schema if requested
|
||||
if strict {
|
||||
apply_strict_validation(&mut schema_value);
|
||||
}
|
||||
|
||||
// Use schema_id directly - simple IDs like "entity", "user", "punc.request"
|
||||
if let Err(e) = compiler.add_resource(schema_id, schema_value.clone()) {
|
||||
errors.push(json!({
|
||||
"code": "SCHEMA_RESOURCE_FAILED",
|
||||
"message": format!("Failed to add schema resource '{}'", schema_id),
|
||||
"details": {
|
||||
"schema": schema_id,
|
||||
"cause": format!("{}", e)
|
||||
}
|
||||
}));
|
||||
return Err(format!("Failed to add schema resource: {}", e));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Helper function to compile all added resources
|
||||
fn compile_all_schemas(
|
||||
compiler: &mut Compiler,
|
||||
cache: &mut BoonCache,
|
||||
schema_ids: &[String],
|
||||
errors: &mut Vec<Value>
|
||||
) -> Result<(), String> {
|
||||
for schema_id in schema_ids {
|
||||
match compiler.compile(schema_id, &mut cache.schemas) {
|
||||
Ok(sch_index) => {
|
||||
// Store the index using the original schema_id as the key
|
||||
cache.id_to_index.insert(schema_id.to_string(), sch_index);
|
||||
}
|
||||
Err(e) => {
|
||||
match &e {
|
||||
CompileError::ValidationError { url: _url, src } => {
|
||||
// Collect leaf errors from the meta-schema validation failure
|
||||
let mut error_list = Vec::new();
|
||||
collect_errors(src, &mut error_list);
|
||||
// Get schema value for error formatting - we'll need to reconstruct or store it
|
||||
let schema_value = json!({}); // Placeholder - we don't have the original value here
|
||||
let formatted_errors = format_errors(error_list, &schema_value, schema_id);
|
||||
errors.extend(formatted_errors);
|
||||
}
|
||||
_ => {
|
||||
// Other compilation errors
|
||||
errors.push(json!({
|
||||
"code": "SCHEMA_COMPILATION_FAILED",
|
||||
"message": format!("Schema '{}' compilation failed", schema_id),
|
||||
"details": {
|
||||
"schema": schema_id,
|
||||
"cause": format!("{:?}", e)
|
||||
}
|
||||
}));
|
||||
}
|
||||
};
|
||||
return Err(format!("Schema compilation failed: {:?}", e));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Helper function to apply strict validation to a schema
|
||||
@ -135,7 +295,7 @@ fn apply_strict_validation_recursive(schema: &mut Value, inside_conditional: boo
|
||||
#[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
|
||||
// Lookup uses the original schema_id - schemas should always be available after bulk caching
|
||||
match cache.id_to_index.get(schema_id) {
|
||||
None => JsonB(json!({
|
||||
"errors": [{
|
||||
@ -143,7 +303,7 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
||||
"message": format!("Schema '{}' not found in cache", schema_id),
|
||||
"details": {
|
||||
"schema": schema_id,
|
||||
"cause": "Schema must be cached before validation"
|
||||
"cause": "Schema was not found in bulk cache - ensure cache_json_schemas was called"
|
||||
}
|
||||
}]
|
||||
})),
|
||||
|
||||
2166
src/tests.rs
2166
src/tests.rs
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user