From c91a53e87d97704de3d03049e3bc5468d9c7d68d Mon Sep 17 00:00:00 2001 From: Dmitry Dygalo Date: Sat, 28 Dec 2024 12:20:20 +0100 Subject: [PATCH] chore: update jsonschema to 0.28.0 Signed-off-by: Dmitry Dygalo --- Cargo.toml | 2 +- src/lib.rs | 40 ++++++++++++++++++++++------------------ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index adc2be9..039e309 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ pg_test = [] pgrx = "0.12.6" serde = "1.0" serde_json = "1.0" -jsonschema = {version = "0.17.1", default-features = false, features = []} +jsonschema = {version = "0.28.0", default-features = false} [dev-dependencies] pgrx-tests = "0.12.6" diff --git a/src/lib.rs b/src/lib.rs index 22f9a1e..b874bdb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,16 +14,14 @@ fn jsonb_matches_schema(schema: Json, instance: JsonB) -> bool { #[pg_extern(immutable, strict, parallel_safe)] fn jsonschema_is_valid(schema: Json) -> bool { - match jsonschema::JSONSchema::compile(&schema.0) { - Ok(_) => true, - Err(e) => { - // Only call notice! for a non empty instance_path - if e.instance_path.last().is_some() { - notice!( - "Invalid JSON schema at path: {}", - e.instance_path.to_string() - ); - } + match jsonschema::meta::try_validate(&schema.0) { + Ok(Ok(_)) => true, + Ok(Err(err)) => { + notice!("Invalid JSON schema at path: {}", err.instance_path); + false + } + Err(err) => { + notice!("{err}"); false } } @@ -31,15 +29,14 @@ fn jsonschema_is_valid(schema: Json) -> bool { #[pg_extern(immutable, strict, parallel_safe)] fn jsonschema_validation_errors(schema: Json, instance: Json) -> Vec { - let schema = match jsonschema::JSONSchema::compile(&schema.0) { - Ok(s) => s, - Err(e) => return vec![e.to_string()], + let validator = match jsonschema::validator_for(&schema.0) { + Ok(v) => v, + Err(err) => return vec![err.to_string()], }; - let errors = match schema.validate(&instance.0) { - Ok(_) => vec![], - Err(e) => e.into_iter().map(|e| e.to_string()).collect(), - }; - errors + validator + .iter_errors(&instance.0) + .map(|err| err.to_string()) + .collect() } #[pg_schema] @@ -146,6 +143,13 @@ mod tests { })))); } + #[pg_test] + fn test_jsonschema_unknown_specification() { + assert!(!crate::jsonschema_is_valid(Json(json!({ + "$schema": "invalid-uri", "type": "string" + })))); + } + #[pg_test] fn test_jsonschema_validation_errors_none() { let errors = crate::jsonschema_validation_errors(