Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b4d9628b05 | |||
| 635d31d723 | |||
| 08efcb92db | |||
| dad1216e1f | |||
| 2fcf8613b8 | |||
| f88c27aa70 | |||
| 48e74815d3 | |||
| 23235d4b9d | |||
| 67406c0b96 | |||
| 28fff3be11 | |||
| 70f3d30258 | |||
| 406466454e | |||
| 2a9d51fa77 | |||
| ae90137308 |
10
.editorconfig
Normal file
10
.editorconfig
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
|
||||||
|
[*.{json,toml,control,rs}]
|
||||||
|
charset = utf-8
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
13
.env
Normal file
13
.env
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
ENVIRONMENT=local
|
||||||
|
DATABASE_PASSWORD=2HwURf1Za7m5ZKtECAfQJGpni3832RV3
|
||||||
|
DATABASE_ROLE=agreego_admin
|
||||||
|
DATABASE_HOST=127.1.27.10
|
||||||
|
DATABASE_PORT=5432
|
||||||
|
POSTGRES_PASSWORD=xzIq5JT0xY3F+2m1GtnrKDdK29sNSXVVYZHPKJVh8pI=
|
||||||
|
DATABASE_NAME=agreego
|
||||||
|
DEV_DATABASE_NAME=agreego_dev
|
||||||
|
GITEA_TOKEN=3d70c23673517330623a5122998fb304e3c73f0a
|
||||||
|
MOOV_ACCOUNT_ID=69a0d2f6-77a2-4e26-934f-d869134f87d3
|
||||||
|
MOOV_PUBLIC_KEY=9OMhK5qGnh7Tmk2Z
|
||||||
|
MOOV_SECRET_KEY=DrRox7B-YWfO9IheiUUX7lGP8-7VY-Ni
|
||||||
|
MOOV_DOMAIN=http://localhost
|
||||||
27
Cargo.lock
generated
27
Cargo.lock
generated
@ -68,6 +68,12 @@ version = "1.0.97"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f"
|
checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "appendlist"
|
||||||
|
version = "1.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e149dc73cd30538307e7ffa2acd3d2221148eaeed4871f246657b1c3eaa1cbd2"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-trait"
|
name = "async-trait"
|
||||||
version = "0.1.88"
|
version = "0.1.88"
|
||||||
@ -177,6 +183,26 @@ dependencies = [
|
|||||||
"generic-array",
|
"generic-array",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "boon"
|
||||||
|
version = "0.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "baa187da765010b70370368c49f08244b1ae5cae1d5d33072f76c8cb7112fe3e"
|
||||||
|
dependencies = [
|
||||||
|
"ahash",
|
||||||
|
"appendlist",
|
||||||
|
"base64",
|
||||||
|
"fluent-uri",
|
||||||
|
"idna",
|
||||||
|
"once_cell",
|
||||||
|
"percent-encoding",
|
||||||
|
"regex",
|
||||||
|
"regex-syntax",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"url",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "borrow-or-share"
|
name = "borrow-or-share"
|
||||||
version = "0.2.2"
|
version = "0.2.2"
|
||||||
@ -1015,6 +1041,7 @@ dependencies = [
|
|||||||
name = "jspg"
|
name = "jspg"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"boon",
|
||||||
"jsonschema",
|
"jsonschema",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"pgrx",
|
"pgrx",
|
||||||
|
|||||||
@ -9,6 +9,7 @@ serde = { version = "1.0", features = ["derive"] }
|
|||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
jsonschema = "0.29.1"
|
jsonschema = "0.29.1"
|
||||||
lazy_static = "1.5.0"
|
lazy_static = "1.5.0"
|
||||||
|
boon = "0.6.1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
pgrx-tests = "0.14.0"
|
pgrx-tests = "0.14.0"
|
||||||
@ -22,6 +23,7 @@ path = "src/bin/pgrx_embed.rs"
|
|||||||
|
|
||||||
[features]
|
[features]
|
||||||
pg17 = ["pgrx/pg17", "pgrx-tests/pg17" ]
|
pg17 = ["pgrx/pg17", "pgrx-tests/pg17" ]
|
||||||
|
# Local feature flag used by `cargo pgrx test`
|
||||||
pg_test = []
|
pg_test = []
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
|
|||||||
36
flow
36
flow
@ -63,7 +63,7 @@ build() {
|
|||||||
|
|
||||||
# Create the source tarball excluding specified patterns
|
# Create the source tarball excluding specified patterns
|
||||||
echo -e " ${CYAN}Creating tarball: ${tarball_path}${RESET}"
|
echo -e " ${CYAN}Creating tarball: ${tarball_path}${RESET}"
|
||||||
if tar --exclude='.git*' --exclude='./target' --exclude='./package' -czf "${tarball_path}" .; then
|
if tar --exclude='.git*' --exclude='./target' --exclude='./package' --exclude='./flows' --exclude='./flow' -czf "${tarball_path}" .; then
|
||||||
echo -e "✨ ${GREEN}Successfully created source tarball: ${tarball_path}${RESET}"
|
echo -e "✨ ${GREEN}Successfully created source tarball: ${tarball_path}${RESET}"
|
||||||
else
|
else
|
||||||
echo -e "❌ ${RED}Failed to create source tarball.${RESET}" >&2
|
echo -e "❌ ${RED}Failed to create source tarball.${RESET}" >&2
|
||||||
@ -76,15 +76,41 @@ install() {
|
|||||||
version=$(get-version) || return 1
|
version=$(get-version) || return 1
|
||||||
|
|
||||||
echo -e "🔧 ${CYAN}Building and installing PGRX extension v$version into local PostgreSQL...${RESET}"
|
echo -e "🔧 ${CYAN}Building and installing PGRX extension v$version into local PostgreSQL...${RESET}"
|
||||||
|
|
||||||
# Run the pgrx install command
|
# Run the pgrx install command
|
||||||
# It implicitly uses --release unless --debug is passed
|
# It implicitly uses --release unless --debug is passed
|
||||||
# It finds pg_config or you can add flags like --pg-config if needed
|
# It finds pg_config or you can add flags like --pg-config if needed
|
||||||
if ! cargo pgrx install "$@"; then # Pass any extra args like --debug
|
if ! cargo pgrx install; then
|
||||||
echo -e "❌ ${RED}cargo pgrx install command failed.${RESET}" >&2
|
echo -e "❌ ${RED}cargo pgrx install command failed.${RESET}" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
echo -e "✨ ${GREEN}PGRX extension v$version successfully built and installed.${RESET}"
|
echo -e "✨ ${GREEN}PGRX extension v$version successfully built and installed.${RESET}"
|
||||||
|
|
||||||
|
# Post-install modification to allow non-superuser usage
|
||||||
|
# Get the installation path dynamically using pg_config
|
||||||
|
local pg_sharedir
|
||||||
|
pg_sharedir=$("$POSTGRES_CONFIG_PATH" --sharedir)
|
||||||
|
if [ -z "$pg_sharedir" ]; then
|
||||||
|
echo -e "❌ ${RED}Failed to determine PostgreSQL shared directory using pg_config.${RESET}" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
local installed_control_path="${pg_sharedir}/extension/jspg.control"
|
||||||
|
|
||||||
|
# Modify the control file
|
||||||
|
if [ ! -f "$installed_control_path" ]; then
|
||||||
|
echo -e "❌ ${RED}Installed control file not found: '$installed_control_path'${RESET}" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "🔧 ${CYAN}Modifying control file for non-superuser access: ${installed_control_path}${RESET}"
|
||||||
|
# Use sed -i '' for macOS compatibility
|
||||||
|
if sed -i '' '/^superuser = false/d' "$installed_control_path" && \
|
||||||
|
echo 'trusted = true' >> "$installed_control_path"; then
|
||||||
|
echo -e "✨ ${GREEN}Control file modified successfully.${RESET}"
|
||||||
|
else
|
||||||
|
echo -e "❌ ${RED}Failed to modify control file: ${installed_control_path}${RESET}" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
test() {
|
test() {
|
||||||
@ -115,8 +141,8 @@ jspg-flow() {
|
|||||||
env) env; return 0;;
|
env) env; return 0;;
|
||||||
prepare) base prepare; cargo-prepare; pgrx-prepare; return 0;;
|
prepare) base prepare; cargo-prepare; pgrx-prepare; return 0;;
|
||||||
build) build; return 0;;
|
build) build; return 0;;
|
||||||
install) base prepare; cargo-prepare; pgrx-prepare; install "$@"; return 0;;
|
install) install; return 0;;
|
||||||
reinstall) base prepare; cargo-prepare; pgrx-prepare; install "$@"; return 0;;
|
reinstall) clean; install; return 0;;
|
||||||
test) test; return 0;;
|
test) test; return 0;;
|
||||||
package) env; package; return 0;;
|
package) env; package; return 0;;
|
||||||
release) env; release; return 0;;
|
release) env; release; return 0;;
|
||||||
|
|||||||
2
flows
2
flows
Submodule flows updated: 6eab4783f3...db55335254
500
src/lib.rs
500
src/lib.rs
@ -1,126 +1,430 @@
|
|||||||
use pgrx::*;
|
use pgrx::*;
|
||||||
use jsonschema::{Draft, Validator};
|
|
||||||
use serde_json::json;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::sync::RwLock;
|
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use jsonschema;
|
|
||||||
|
|
||||||
pg_module_magic!();
|
pg_module_magic!();
|
||||||
|
|
||||||
// Global, thread-safe schema cache using the correct Validator type
|
use serde_json::{json, Value};
|
||||||
|
use std::{collections::HashMap, sync::RwLock};
|
||||||
|
use boon::{Compiler, Schemas, ValidationError, SchemaIndex, CompileError};
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
|
struct BoonCache {
|
||||||
|
schemas: Schemas,
|
||||||
|
id_to_index: HashMap<String, SchemaIndex>,
|
||||||
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref SCHEMA_CACHE: RwLock<HashMap<String, Validator>> = RwLock::new(HashMap::new());
|
static ref SCHEMA_CACHE: RwLock<BoonCache> = RwLock::new(BoonCache {
|
||||||
|
schemas: Schemas::new(),
|
||||||
|
id_to_index: HashMap::new(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache a schema explicitly with a provided ID
|
#[pg_extern(strict)]
|
||||||
#[pg_extern(immutable, strict, parallel_safe)]
|
fn cache_json_schema(schema_id: &str, schema: JsonB) -> JsonB {
|
||||||
fn cache_schema(schema_id: &str, schema: JsonB) -> bool {
|
let mut cache = SCHEMA_CACHE.write().unwrap();
|
||||||
match jsonschema::options()
|
let schema_value: Value = schema.0;
|
||||||
.with_draft(Draft::Draft7)
|
|
||||||
.should_validate_formats(true)
|
let mut compiler = Compiler::new();
|
||||||
.build(&schema.0)
|
compiler.enable_format_assertions();
|
||||||
{
|
|
||||||
Ok(compiled) => {
|
if let Err(e) = compiler.add_resource(schema_id, schema_value) {
|
||||||
SCHEMA_CACHE.write().unwrap().insert(schema_id.to_string(), compiled);
|
return JsonB(json!({
|
||||||
true
|
"success": false,
|
||||||
},
|
"error": {
|
||||||
Err(e) => {
|
"kind": "SchemaResourceError",
|
||||||
notice!("Failed to cache schema '{}': {}", schema_id, e);
|
"message": format!("Failed to add schema resource: {}", e),
|
||||||
false
|
"schema_id": schema_id
|
||||||
}
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
match compiler.compile(schema_id, &mut cache.schemas) {
|
||||||
|
Ok(sch_index) => {
|
||||||
|
cache.id_to_index.insert(schema_id.to_string(), sch_index);
|
||||||
|
JsonB(json!({ "success": true }))
|
||||||
}
|
}
|
||||||
}
|
Err(e) => {
|
||||||
|
// Enhance error reporting by matching on the CompileError variant
|
||||||
// Check if a schema is cached
|
let error_details = match &e {
|
||||||
#[pg_extern(immutable, strict, parallel_safe)]
|
CompileError::ValidationError { url, src } => {
|
||||||
fn schema_cached(schema_id: &str) -> bool {
|
// Metaschema validation failed - provide more detail
|
||||||
SCHEMA_CACHE.read().unwrap().contains_key(schema_id)
|
json!({
|
||||||
}
|
"kind": "SchemaCompilationError",
|
||||||
|
"sub_kind": "ValidationError", // Explicitly state it's a metaschema validation error
|
||||||
// Validate JSONB instance against a cached schema by ID
|
"message": format!("Schema failed validation against its metaschema: {}", src),
|
||||||
#[pg_extern(immutable, strict, parallel_safe)]
|
"schema_id": schema_id,
|
||||||
fn validate_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
"failed_at_url": url,
|
||||||
let cache = SCHEMA_CACHE.read().unwrap();
|
"validation_details": format!("{:?}", src), // Include full debug info of the validation error
|
||||||
let compiled_schema: &Validator = match cache.get(schema_id) {
|
})
|
||||||
Some(schema) => schema,
|
}
|
||||||
None => {
|
// Handle other potential compilation errors
|
||||||
return JsonB(json!({
|
_ => {
|
||||||
"valid": false,
|
let error_type = format!("{:?}", e).split('(').next().unwrap_or("Unknown").to_string();
|
||||||
"errors": [format!("Schema ID '{}' not cached", schema_id)]
|
json!({
|
||||||
}));
|
"kind": "SchemaCompilationError",
|
||||||
}
|
"sub_kind": error_type, // e.g., "InvalidJsonPointer", "UnsupportedUrlScheme"
|
||||||
};
|
"message": format!("Schema compilation failed: {}", e),
|
||||||
|
"schema_id": schema_id,
|
||||||
if compiled_schema.is_valid(&instance.0) {
|
"details": format!("{:?}", e), // Generic debug info
|
||||||
JsonB(json!({ "valid": true }))
|
})
|
||||||
} else {
|
}
|
||||||
let errors: Vec<String> = compiled_schema
|
};
|
||||||
.iter_errors(&instance.0)
|
JsonB(json!({
|
||||||
.map(|e| e.to_string())
|
"success": false,
|
||||||
.collect();
|
"error": error_details
|
||||||
|
}))
|
||||||
JsonB(json!({ "valid": false, "errors": errors }))
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear the entire schema cache explicitly
|
#[pg_extern(strict, parallel_safe)]
|
||||||
#[pg_extern(immutable, parallel_safe)]
|
fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
||||||
fn clear_schema_cache() -> bool {
|
let cache = SCHEMA_CACHE.read().unwrap();
|
||||||
SCHEMA_CACHE.write().unwrap().clear();
|
|
||||||
true
|
match cache.id_to_index.get(schema_id) {
|
||||||
|
None => JsonB(json!({
|
||||||
|
"success": false,
|
||||||
|
"error": {
|
||||||
|
"kind": "SchemaNotFound",
|
||||||
|
"message": format!("Schema with id '{}' not found in cache", schema_id)
|
||||||
|
}
|
||||||
|
})),
|
||||||
|
Some(sch_index) => {
|
||||||
|
let instance_value: Value = instance.0;
|
||||||
|
match cache.schemas.validate(&instance_value, *sch_index) {
|
||||||
|
Ok(_) => JsonB(json!({ "success": true })),
|
||||||
|
Err(validation_error) => {
|
||||||
|
let error = format_validation_error(&validation_error);
|
||||||
|
JsonB(json!({
|
||||||
|
"success": false,
|
||||||
|
"error": error
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn format_validation_error(error: &ValidationError) -> Value {
|
||||||
|
json!({
|
||||||
|
"instance_path": error.instance_location.to_string(),
|
||||||
|
"schema_path": error.schema_url.to_string(),
|
||||||
|
"kind": format!("{:?}", error.kind),
|
||||||
|
"message": format!("{}", error),
|
||||||
|
"error": error
|
||||||
|
.causes
|
||||||
|
.iter()
|
||||||
|
.map(format_validation_error)
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pg_extern(strict, parallel_safe)]
|
||||||
|
fn json_schema_cached(schema_id: &str) -> bool {
|
||||||
|
let cache = SCHEMA_CACHE.read().unwrap();
|
||||||
|
cache.id_to_index.contains_key(schema_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pg_extern(strict)]
|
||||||
|
fn clear_json_schemas() {
|
||||||
|
let mut cache = SCHEMA_CACHE.write().unwrap();
|
||||||
|
*cache = BoonCache {
|
||||||
|
schemas: Schemas::new(),
|
||||||
|
id_to_index: HashMap::new(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pg_extern(strict, parallel_safe)]
|
||||||
|
fn show_json_schemas() -> Vec<String> {
|
||||||
|
let cache = SCHEMA_CACHE.read().unwrap();
|
||||||
|
let ids: Vec<String> = cache.id_to_index.keys().cloned().collect();
|
||||||
|
ids
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pg_schema]
|
#[pg_schema]
|
||||||
#[cfg(any(test, feature = "pg_test"))]
|
#[cfg(any(test, feature = "pg_test"))]
|
||||||
mod tests {
|
mod tests {
|
||||||
use pgrx::*;
|
use pgrx::*;
|
||||||
use serde_json::json;
|
use pgrx::pg_test;
|
||||||
|
use super::*;
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
#[pg_test]
|
fn jsonb(val: Value) -> JsonB {
|
||||||
fn test_cache_and_validate_schema() {
|
JsonB(val)
|
||||||
assert!(crate::cache_schema("test_schema", JsonB(json!({ "type": "object" }))));
|
}
|
||||||
assert!(crate::schema_cached("test_schema"));
|
|
||||||
|
|
||||||
let result_valid = crate::validate_schema("test_schema", JsonB(json!({ "foo": "bar" })));
|
fn setup_test() {
|
||||||
assert_eq!(result_valid.0["valid"], true);
|
clear_json_schemas();
|
||||||
|
}
|
||||||
|
|
||||||
let result_invalid = crate::validate_schema("test_schema", JsonB(json!(42)));
|
#[pg_test]
|
||||||
assert_eq!(result_invalid.0["valid"], false);
|
fn test_cache_and_validate_json_schema() {
|
||||||
assert!(result_invalid.0["errors"][0].as_str().unwrap().contains("not of type"));
|
setup_test();
|
||||||
}
|
let schema_id = "my_schema";
|
||||||
|
let schema = json!({
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"name": { "type": "string" },
|
||||||
|
"age": { "type": "integer", "minimum": 0 }
|
||||||
|
},
|
||||||
|
"required": ["name", "age"]
|
||||||
|
});
|
||||||
|
let valid_instance = json!({ "name": "Alice", "age": 30 });
|
||||||
|
let invalid_instance_type = json!({ "name": "Bob", "age": -5 });
|
||||||
|
let invalid_instance_missing = json!({ "name": "Charlie" });
|
||||||
|
|
||||||
#[pg_test]
|
let cache_result = cache_json_schema(schema_id, jsonb(schema.clone()));
|
||||||
fn test_schema_not_cached() {
|
assert!(cache_result.0["success"].as_bool().unwrap());
|
||||||
let result = crate::validate_schema("unknown_schema", JsonB(json!({})));
|
|
||||||
assert_eq!(result.0["valid"], false);
|
|
||||||
assert!(result.0["errors"][0].as_str().unwrap().contains("not cached"));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[pg_test]
|
let valid_result = validate_json_schema(schema_id, jsonb(valid_instance));
|
||||||
fn test_clear_schema_cache() {
|
assert!(valid_result.0["success"].as_bool().unwrap());
|
||||||
crate::cache_schema("clear_test", JsonB(json!({ "type": "object" })));
|
|
||||||
assert!(crate::schema_cached("clear_test"));
|
|
||||||
|
|
||||||
crate::clear_schema_cache();
|
let invalid_result_type = validate_json_schema(schema_id, jsonb(invalid_instance_type));
|
||||||
assert!(!crate::schema_cached("clear_test"));
|
assert!(!invalid_result_type.0["success"].as_bool().unwrap());
|
||||||
}
|
|
||||||
|
|
||||||
#[pg_test]
|
let error_obj_type = invalid_result_type.0.get("error").expect("Expected top-level 'error' object");
|
||||||
fn test_invalid_schema_cache() {
|
let causes_age = error_obj_type.get("error").and_then(Value::as_array).expect("Expected nested 'error' array (causes)");
|
||||||
let result = crate::cache_schema("bad_schema", JsonB(json!({ "type": "unknown_type" })));
|
assert!(!causes_age.is_empty(), "Expected causes for invalid age");
|
||||||
assert!(!result);
|
let first_cause_age = &causes_age[0];
|
||||||
assert!(!crate::schema_cached("bad_schema"));
|
assert!(first_cause_age["kind"].as_str().unwrap().contains("Minimum"), "Kind '{}' should contain Minimum", first_cause_age["kind"]);
|
||||||
}
|
let msg = first_cause_age["message"].as_str().unwrap_or("");
|
||||||
|
assert!(msg.contains("must be >=0"), "Error message mismatch for age minimum: {}", msg);
|
||||||
|
|
||||||
|
let invalid_result_missing = validate_json_schema(schema_id, jsonb(invalid_instance_missing));
|
||||||
|
assert!(!invalid_result_missing.0["success"].as_bool().unwrap());
|
||||||
|
|
||||||
|
let error_obj_missing = invalid_result_missing.0.get("error").expect("Expected top-level 'error' object");
|
||||||
|
let causes_missing = error_obj_missing.get("error").and_then(Value::as_array).expect("Expected nested 'error' array (causes) for missing");
|
||||||
|
assert!(!causes_missing.is_empty(), "Expected causes for missing age");
|
||||||
|
let first_cause_missing = &causes_missing[0];
|
||||||
|
assert!(first_cause_missing["kind"].as_str().unwrap().contains("Required"));
|
||||||
|
let msg_missing = first_cause_missing["message"].as_str().unwrap_or("");
|
||||||
|
assert!(msg_missing.contains("missing properties 'age'"), "Error message mismatch for missing 'age': {}", msg_missing);
|
||||||
|
assert!(first_cause_missing["instance_path"] == "", "Expected empty instance path for missing field");
|
||||||
|
|
||||||
|
let non_existent_id = "non_existent_schema";
|
||||||
|
let invalid_schema_result = validate_json_schema(non_existent_id, jsonb(json!({})));
|
||||||
|
assert!(!invalid_schema_result.0["success"].as_bool().unwrap());
|
||||||
|
let schema_not_found_error = invalid_schema_result.0
|
||||||
|
.get("error") // Top level error object
|
||||||
|
.expect("Expected top-level 'error' object for schema not found");
|
||||||
|
assert_eq!(schema_not_found_error["kind"], "SchemaNotFound");
|
||||||
|
assert!(schema_not_found_error["message"].as_str().unwrap().contains(non_existent_id));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pg_test]
|
||||||
|
fn test_validate_json_schema_not_cached() {
|
||||||
|
setup_test();
|
||||||
|
let instance = json!({ "foo": "bar" });
|
||||||
|
let result = validate_json_schema("non_existent_schema", jsonb(instance));
|
||||||
|
assert!(!result.0["success"].as_bool().unwrap());
|
||||||
|
let error_obj = result.0.get("error").expect("Expected top-level 'error' object");
|
||||||
|
assert_eq!(error_obj["kind"], "SchemaNotFound");
|
||||||
|
assert!(error_obj["message"].as_str().unwrap().contains("non_existent_schema"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pg_test]
|
||||||
|
fn test_cache_invalid_json_schema() {
|
||||||
|
setup_test();
|
||||||
|
let schema_id = "invalid_schema";
|
||||||
|
let invalid_schema_json = "{\"type\": \"string\" \"maxLength\": 5}";
|
||||||
|
let invalid_schema_value: Result<Value, _> = serde_json::from_str(invalid_schema_json);
|
||||||
|
assert!(invalid_schema_value.is_err(), "Test setup assumes invalid JSON string");
|
||||||
|
|
||||||
|
let schema_representing_invalid = json!({
|
||||||
|
"type": 123
|
||||||
|
});
|
||||||
|
|
||||||
|
let result = cache_json_schema(schema_id, jsonb(schema_representing_invalid.clone()));
|
||||||
|
assert!(!result.0["success"].as_bool().unwrap());
|
||||||
|
let error_obj = result.0.get("error").expect("Expected top-level 'error' object for compilation failure");
|
||||||
|
assert_eq!(error_obj.get("kind").and_then(Value::as_str), Some("SchemaCompilationError"));
|
||||||
|
assert_eq!(error_obj.get("sub_kind").and_then(Value::as_str), Some("ValidationError"), "Expected sub_kind 'ValidationError' for metaschema failure");
|
||||||
|
assert!(error_obj.get("message").and_then(Value::as_str).is_some(), "Expected 'message' field in error object");
|
||||||
|
assert!(error_obj["message"].as_str().unwrap().contains("Schema failed validation against its metaschema"), "Error message mismatch");
|
||||||
|
assert_eq!(error_obj.get("schema_id").and_then(Value::as_str), Some(schema_id));
|
||||||
|
let failed_at_url = error_obj.get("failed_at_url").and_then(Value::as_str).expect("Expected 'failed_at_url' string");
|
||||||
|
assert!(failed_at_url.ends_with(&format!("{}#", schema_id)), "failed_at_url ('{}') should end with schema_id + '#' ('{}#')", failed_at_url, schema_id);
|
||||||
|
assert!(error_obj.get("validation_details").and_then(Value::as_str).is_some(), "Expected 'validation_details' field");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pg_test]
|
||||||
|
fn test_validate_json_schema_detailed_validation_errors() {
|
||||||
|
setup_test();
|
||||||
|
let schema_id = "detailed_schema";
|
||||||
|
let schema = json!({
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"address": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"street": { "type": "string" },
|
||||||
|
"city": { "type": "string", "maxLength": 10 }
|
||||||
|
},
|
||||||
|
"required": ["street", "city"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["address"]
|
||||||
|
});
|
||||||
|
let invalid_instance = json!({
|
||||||
|
"address": {
|
||||||
|
"street": 123,
|
||||||
|
"city": "Supercalifragilisticexpialidocious"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
assert!(cache_json_schema(schema_id, jsonb(schema.clone())).0["success"].as_bool().unwrap());
|
||||||
|
let result = validate_json_schema(schema_id, jsonb(invalid_instance));
|
||||||
|
assert!(!result.0["success"].as_bool().unwrap());
|
||||||
|
|
||||||
|
let error_obj = result.0.get("error").expect("Expected top-level 'error' object");
|
||||||
|
let causes = error_obj.get("error").and_then(Value::as_array).expect("Expected nested 'error' array (causes)");
|
||||||
|
assert!(causes.len() >= 2, "Expected at least 2 detailed causes");
|
||||||
|
|
||||||
|
let street_error = causes.iter().find(|e| e["instance_path"] == "/address/street").expect("Missing street error");
|
||||||
|
assert!(street_error["kind"].as_str().unwrap().contains("Type"), "Kind '{}' should contain Type", street_error["kind"]);
|
||||||
|
let street_msg = street_error["message"].as_str().unwrap_or("null");
|
||||||
|
assert!(street_msg.contains("want string, but got number"), "Street message mismatch: {}", street_msg);
|
||||||
|
|
||||||
|
let city_error = causes.iter().find(|e| e["instance_path"] == "/address/city").expect("Missing city error");
|
||||||
|
assert!(city_error["kind"].as_str().unwrap().contains("MaxLength"), "Kind '{}' should contain MaxLength", city_error["kind"]);
|
||||||
|
let city_msg = city_error["message"].as_str().unwrap_or("null");
|
||||||
|
assert!(city_msg.contains("length must be <=10"), "City message mismatch: {}", city_msg);
|
||||||
|
|
||||||
|
assert_eq!(causes.len(), 2, "Expected exactly 2 errors (street type, city length)");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pg_test]
|
||||||
|
fn test_validate_json_schema_oneof_validation_errors() {
|
||||||
|
setup_test();
|
||||||
|
let schema_id = "oneof_schema";
|
||||||
|
let schema = json!({
|
||||||
|
"oneOf": [
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"string_prop": { "type": "string", "maxLength": 5 }
|
||||||
|
},
|
||||||
|
"required": ["string_prop"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"number_prop": { "type": "number", "minimum": 10 }
|
||||||
|
},
|
||||||
|
"required": ["number_prop"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
cache_json_schema(schema_id, jsonb(schema));
|
||||||
|
|
||||||
|
let invalid_string_instance = json!({ "string_prop": "toolongstring" });
|
||||||
|
let result_invalid_string = validate_json_schema(schema_id, jsonb(invalid_string_instance));
|
||||||
|
assert!(!result_invalid_string.0["success"].as_bool().unwrap());
|
||||||
|
|
||||||
|
let error_obj_string = result_invalid_string.0.get("error").expect("Expected top-level 'error' object");
|
||||||
|
assert!(error_obj_string["kind"].as_str().unwrap().contains("Schema"), "Top level kind '{}' should contain Schema for OneOf failure", error_obj_string["kind"]);
|
||||||
|
assert!(error_obj_string["message"].as_str().unwrap().contains("oneOf failed, none matched"), "OneOf message mismatch: {}", error_obj_string["message"]); // Final adjustment
|
||||||
|
|
||||||
|
let causes_string = error_obj_string.get("error").and_then(Value::as_array).expect("Expected nested 'error' array (causes)");
|
||||||
|
assert_eq!(causes_string.len(), 1, "Expected one cause for oneOf failure (string)");
|
||||||
|
|
||||||
|
let nested_causes_string = causes_string[0].get("error").and_then(Value::as_array).expect("Expected deeper nested causes for string oneOf");
|
||||||
|
assert_eq!(nested_causes_string.len(), 2, "Expected two nested causes for string oneOf");
|
||||||
|
let string_schema_fail = nested_causes_string.iter().find(|c| c["schema_path"].as_str().unwrap().ends_with("/oneOf/0/properties/string_prop")).expect("Missing nested cause for string schema");
|
||||||
|
assert_eq!(string_schema_fail["instance_path"].as_str().unwrap(), "/string_prop", "Instance path should be /string_prop");
|
||||||
|
assert!(string_schema_fail["kind"].as_str().unwrap().contains("MaxLength"), "Nested string cause kind should be MaxLength");
|
||||||
|
let number_schema_fail = nested_causes_string.iter().find(|c| c["schema_path"].as_str().unwrap().ends_with("/oneOf/1")).expect("Missing nested cause for number schema");
|
||||||
|
assert_eq!(number_schema_fail["instance_path"].as_str().unwrap(), "", "Instance path for branch 2 type mismatch should be empty");
|
||||||
|
assert!(number_schema_fail["kind"].as_str().unwrap().contains("Required"), "Nested number cause kind should be Required");
|
||||||
|
|
||||||
|
let invalid_number_instance = json!({ "number_prop": 5 });
|
||||||
|
let result_invalid_number = validate_json_schema(schema_id, jsonb(invalid_number_instance));
|
||||||
|
assert!(!result_invalid_number.0["success"].as_bool().unwrap());
|
||||||
|
|
||||||
|
let error_obj_number = result_invalid_number.0.get("error").expect("Expected top-level 'error' object");
|
||||||
|
assert!(error_obj_number["kind"].as_str().unwrap().contains("Schema"), "Top level kind '{}' should contain Schema for OneOf failure", error_obj_number["kind"]);
|
||||||
|
assert!(error_obj_number["message"].as_str().unwrap().contains("oneOf failed, none matched"), "OneOf message mismatch: {}", error_obj_number["message"]); // Final adjustment
|
||||||
|
|
||||||
|
let causes_number = error_obj_number.get("error").and_then(Value::as_array).expect("Expected nested 'error' array (causes)");
|
||||||
|
assert_eq!(causes_number.len(), 1, "Expected one cause for oneOf failure (number)");
|
||||||
|
let nested_causes_number = causes_number[0].get("error").and_then(Value::as_array).expect("Expected deeper nested causes for number oneOf");
|
||||||
|
assert_eq!(nested_causes_number.len(), 2, "Expected two nested causes for number oneOf");
|
||||||
|
let string_schema_fail_num = nested_causes_number.iter().find(|c| c["schema_path"].as_str().unwrap().ends_with("/oneOf/0")).expect("Missing nested cause for string schema (number case)");
|
||||||
|
assert_eq!(string_schema_fail_num["instance_path"].as_str().unwrap(), "", "Instance path for branch 1 type mismatch should be empty");
|
||||||
|
assert!(string_schema_fail_num["kind"].as_str().unwrap().contains("Required"), "Nested string cause kind should be Required (number case)");
|
||||||
|
let number_schema_fail_num = nested_causes_number.iter().find(|c| c["schema_path"].as_str().unwrap().ends_with("/oneOf/1/properties/number_prop")).expect("Missing nested cause for number schema (number case)");
|
||||||
|
assert_eq!(number_schema_fail_num["instance_path"].as_str().unwrap(), "/number_prop", "Instance path should be /number_prop (number case)");
|
||||||
|
assert!(number_schema_fail_num["kind"].as_str().unwrap().contains("Minimum"), "Nested number cause kind should be Minimum (number case)");
|
||||||
|
|
||||||
|
let invalid_bool_instance = json!({ "other_prop": true });
|
||||||
|
let result_invalid_bool = validate_json_schema(schema_id, jsonb(invalid_bool_instance));
|
||||||
|
assert!(!result_invalid_bool.0["success"].as_bool().unwrap());
|
||||||
|
|
||||||
|
let error_obj_bool = result_invalid_bool.0.get("error").expect("Expected top-level 'error' object");
|
||||||
|
assert!(error_obj_bool["kind"].as_str().unwrap().contains("Schema"), "Top level kind '{}' should contain Schema for OneOf failure", error_obj_bool["kind"]);
|
||||||
|
assert!(error_obj_bool["message"].as_str().unwrap().contains("oneOf failed, none matched"), "OneOf message mismatch: {}", error_obj_bool["message"]); // Final adjustment
|
||||||
|
|
||||||
|
let causes_bool = error_obj_bool.get("error").and_then(Value::as_array).expect("Expected nested 'error' array (causes)");
|
||||||
|
assert_eq!(causes_bool.len(), 1, "Expected one cause for oneOf failure (bool)");
|
||||||
|
let nested_causes_bool = causes_bool[0].get("error").and_then(Value::as_array).expect("Expected deeper nested causes for bool oneOf");
|
||||||
|
assert_eq!(nested_causes_bool.len(), 2, "Expected two nested causes for bool oneOf");
|
||||||
|
let bool_fail_0 = nested_causes_bool.iter().find(|c| c["schema_path"].as_str().unwrap().ends_with("/oneOf/0")).expect("Missing nested cause for branch 0 type fail");
|
||||||
|
assert_eq!(bool_fail_0["instance_path"].as_str().unwrap(), "", "Instance path for branch 0 type fail should be empty");
|
||||||
|
assert!(bool_fail_0["kind"].as_str().unwrap().contains("Required"), "Nested bool cause 0 kind should be Required");
|
||||||
|
let bool_fail_1 = nested_causes_bool.iter().find(|c| c["schema_path"].as_str().unwrap().ends_with("/oneOf/1")).expect("Missing nested cause for branch 1 type fail");
|
||||||
|
assert_eq!(bool_fail_1["instance_path"].as_str().unwrap(), "", "Instance path for branch 1 type fail should be empty");
|
||||||
|
assert!(bool_fail_1["kind"].as_str().unwrap().contains("Required"), "Nested bool cause 1 kind should be Required");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pg_test]
|
||||||
|
fn test_clear_json_schemas() {
|
||||||
|
setup_test();
|
||||||
|
let schema_id = "schema_to_clear";
|
||||||
|
let schema = json!({ "type": "string" });
|
||||||
|
cache_json_schema(schema_id, jsonb(schema.clone()));
|
||||||
|
|
||||||
|
let show_result1 = show_json_schemas();
|
||||||
|
assert!(show_result1.contains(&schema_id.to_string()));
|
||||||
|
|
||||||
|
clear_json_schemas();
|
||||||
|
|
||||||
|
let show_result2 = show_json_schemas();
|
||||||
|
assert!(show_result2.is_empty());
|
||||||
|
|
||||||
|
let instance = json!("test");
|
||||||
|
let validate_result = validate_json_schema(schema_id, jsonb(instance));
|
||||||
|
assert!(!validate_result.0["success"].as_bool().unwrap());
|
||||||
|
let error_obj = validate_result.0.get("error").expect("Expected top-level 'error' object");
|
||||||
|
assert_eq!(error_obj["kind"], "SchemaNotFound");
|
||||||
|
assert!(error_obj["message"].as_str().unwrap().contains(schema_id));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[pg_test]
|
||||||
|
fn test_show_json_schemas() {
|
||||||
|
setup_test();
|
||||||
|
let schema_id1 = "schema1";
|
||||||
|
let schema_id2 = "schema2";
|
||||||
|
let schema = json!({ "type": "boolean" });
|
||||||
|
|
||||||
|
cache_json_schema(schema_id1, jsonb(schema.clone()));
|
||||||
|
cache_json_schema(schema_id2, jsonb(schema.clone()));
|
||||||
|
|
||||||
|
let result = show_json_schemas();
|
||||||
|
assert!(result.contains(&schema_id1.to_string()));
|
||||||
|
assert!(result.contains(&schema_id2.to_string()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod pg_test {
|
pub mod pg_test {
|
||||||
pub fn setup(_options: Vec<&str>) {
|
pub fn setup(_options: Vec<&str>) {
|
||||||
// Initialization if needed
|
// perform one-off initialization when the pg_test framework starts
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn postgresql_conf_options() -> Vec<&'static str> {
|
pub fn postgresql_conf_options() -> Vec<&'static str> {
|
||||||
vec![]
|
// return any postgresql.conf settings that are required for your tests
|
||||||
}
|
vec![]
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user