library test suite for drop validation, fixed drop return structures
This commit is contained in:
@ -13,7 +13,7 @@ pub struct Drop {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub response: Option<Value>,
|
||||
|
||||
#[serde(default)]
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub errors: Vec<Error>,
|
||||
}
|
||||
|
||||
@ -29,7 +29,7 @@ impl Drop {
|
||||
pub fn success() -> Self {
|
||||
Self {
|
||||
type_: "drop".to_string(),
|
||||
response: Some(serde_json::json!({ "result": "success" })), // Or appropriate success response
|
||||
response: Some(serde_json::json!("success")),
|
||||
errors: vec![],
|
||||
}
|
||||
}
|
||||
@ -53,8 +53,6 @@ impl Drop {
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct Error {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub punc: Option<String>,
|
||||
pub code: String,
|
||||
pub message: String,
|
||||
pub details: ErrorDetails,
|
||||
|
||||
68
src/lib.rs
68
src/lib.rs
@ -25,7 +25,7 @@ lazy_static::lazy_static! {
|
||||
}
|
||||
|
||||
#[pg_extern(strict)]
|
||||
fn cache_json_schemas(enums: JsonB, types: JsonB, puncs: JsonB) -> JsonB {
|
||||
pub fn cache_json_schemas(enums: JsonB, types: JsonB, puncs: JsonB) -> JsonB {
|
||||
// 1. Build a new Registry LOCALLY (on stack)
|
||||
let mut registry = registry::Registry::new();
|
||||
|
||||
@ -107,11 +107,12 @@ fn cache_json_schemas(enums: JsonB, types: JsonB, puncs: JsonB) -> JsonB {
|
||||
*lock = Some(new_arc);
|
||||
}
|
||||
|
||||
JsonB(json!({ "response": "success" }))
|
||||
let drop = crate::drop::Drop::success();
|
||||
JsonB(serde_json::to_value(drop).unwrap())
|
||||
}
|
||||
|
||||
#[pg_extern(strict, parallel_safe)]
|
||||
fn mask_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
||||
pub fn mask_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
||||
// 1. Acquire Snapshot
|
||||
let validator_arc = {
|
||||
let lock = GLOBAL_VALIDATOR.read().unwrap();
|
||||
@ -135,7 +136,6 @@ fn mask_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
||||
.errors
|
||||
.into_iter()
|
||||
.map(|e| crate::drop::Error {
|
||||
punc: None,
|
||||
code: e.code,
|
||||
message: e.message,
|
||||
details: crate::drop::ErrorDetails { path: e.path },
|
||||
@ -148,7 +148,6 @@ fn mask_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
||||
Err(e) => {
|
||||
// Schema Not Found or other fatal error
|
||||
let error = crate::drop::Error {
|
||||
punc: None,
|
||||
code: e.code,
|
||||
message: e.message,
|
||||
details: crate::drop::ErrorDetails { path: e.path },
|
||||
@ -158,19 +157,20 @@ fn mask_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
JsonB(json!({
|
||||
"punc": null,
|
||||
"errors": [{
|
||||
"code": "VALIDATOR_NOT_INITIALIZED",
|
||||
"message": "JSON Schemas have not been cached yet. Run cache_json_schemas()",
|
||||
"details": { "path": "" }
|
||||
}]
|
||||
}))
|
||||
let error = crate::drop::Error {
|
||||
code: "VALIDATOR_NOT_INITIALIZED".to_string(),
|
||||
message: "JSON Schemas have not been cached yet. Run cache_json_schemas()".to_string(),
|
||||
details: crate::drop::ErrorDetails {
|
||||
path: "".to_string(),
|
||||
},
|
||||
};
|
||||
let drop = crate::drop::Drop::with_errors(vec![error]);
|
||||
JsonB(serde_json::to_value(drop).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
#[pg_extern(strict, parallel_safe)]
|
||||
fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
||||
pub fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
||||
// 1. Acquire Snapshot
|
||||
let validator_arc = {
|
||||
let lock = GLOBAL_VALIDATOR.read().unwrap();
|
||||
@ -189,7 +189,6 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
||||
.errors
|
||||
.into_iter()
|
||||
.map(|e| crate::drop::Error {
|
||||
punc: None,
|
||||
code: e.code,
|
||||
message: e.message,
|
||||
details: crate::drop::ErrorDetails { path: e.path },
|
||||
@ -201,7 +200,6 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
||||
}
|
||||
Err(e) => {
|
||||
let error = crate::drop::Error {
|
||||
punc: None,
|
||||
code: e.code,
|
||||
message: e.message,
|
||||
details: crate::drop::ErrorDetails { path: e.path },
|
||||
@ -211,19 +209,20 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
JsonB(json!({
|
||||
"punc": null,
|
||||
"errors": [{
|
||||
"code": "VALIDATOR_NOT_INITIALIZED",
|
||||
"message": "JSON Schemas have not been cached yet. Run cache_json_schemas()",
|
||||
"details": { "path": "" }
|
||||
}]
|
||||
}))
|
||||
let error = crate::drop::Error {
|
||||
code: "VALIDATOR_NOT_INITIALIZED".to_string(),
|
||||
message: "JSON Schemas have not been cached yet. Run cache_json_schemas()".to_string(),
|
||||
details: crate::drop::ErrorDetails {
|
||||
path: "".to_string(),
|
||||
},
|
||||
};
|
||||
let drop = crate::drop::Drop::with_errors(vec![error]);
|
||||
JsonB(serde_json::to_value(drop).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
#[pg_extern(strict, parallel_safe)]
|
||||
fn json_schema_cached(schema_id: &str) -> bool {
|
||||
pub fn json_schema_cached(schema_id: &str) -> bool {
|
||||
if let Some(validator) = GLOBAL_VALIDATOR.read().unwrap().as_ref() {
|
||||
match validator.validate(schema_id, &serde_json::Value::Null) {
|
||||
Err(e) if e.code == "SCHEMA_NOT_FOUND" => false,
|
||||
@ -235,18 +234,23 @@ fn json_schema_cached(schema_id: &str) -> bool {
|
||||
}
|
||||
|
||||
#[pg_extern(strict)]
|
||||
fn clear_json_schemas() -> JsonB {
|
||||
pub fn clear_json_schemas() -> JsonB {
|
||||
let mut lock = GLOBAL_VALIDATOR.write().unwrap();
|
||||
*lock = None;
|
||||
JsonB(json!({ "response": "success" }))
|
||||
let drop = crate::drop::Drop::success();
|
||||
JsonB(serde_json::to_value(drop).unwrap())
|
||||
}
|
||||
|
||||
#[pg_extern(strict, parallel_safe)]
|
||||
fn show_json_schemas() -> JsonB {
|
||||
if let Some(_validator) = GLOBAL_VALIDATOR.read().unwrap().as_ref() {
|
||||
JsonB(json!({ "response": "success", "status": "active" }))
|
||||
pub fn show_json_schemas() -> JsonB {
|
||||
if let Some(validator) = GLOBAL_VALIDATOR.read().unwrap().as_ref() {
|
||||
let mut keys = validator.get_schema_ids();
|
||||
keys.sort();
|
||||
let drop = crate::drop::Drop::success_with_val(json!(keys));
|
||||
JsonB(serde_json::to_value(drop).unwrap())
|
||||
} else {
|
||||
JsonB(json!({ "response": "success", "status": "empty" }))
|
||||
let drop = crate::drop::Drop::success_with_val(json!([]));
|
||||
JsonB(serde_json::to_value(drop).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
@ -254,7 +258,7 @@ fn show_json_schemas() -> JsonB {
|
||||
#[pg_schema]
|
||||
mod tests {
|
||||
use pgrx::prelude::*;
|
||||
include!("tests.rs");
|
||||
include!("tests/fixtures.rs");
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@ -1279,6 +1279,10 @@ impl Validator {
|
||||
Self { registry }
|
||||
}
|
||||
|
||||
pub fn get_schema_ids(&self) -> Vec<String> {
|
||||
self.registry.schemas.keys().cloned().collect()
|
||||
}
|
||||
|
||||
pub fn check_type(t: &str, val: &Value) -> bool {
|
||||
if let Value::String(s) = val {
|
||||
if s.is_empty() {
|
||||
|
||||
Reference in New Issue
Block a user