From ce503b1fe7fe4972a265b25264811c4f6eab1a02 Mon Sep 17 00:00:00 2001 From: "David E. Wheeler" Date: Fri, 12 Apr 2024 16:11:10 -0400 Subject: [PATCH] Add jsonb?_matches_schema tests --- src/lib.rs | 2 +- test/expected/is_valid.out | 13 -------- test/expected/matches.out | 68 ++++++++++++++++++++++++++++++++++++++ test/sql/is_valid.sql | 6 ---- test/sql/matches.sql | 21 ++++++++++++ test/sql/validates.sql | 1 + 6 files changed, 91 insertions(+), 20 deletions(-) create mode 100644 test/expected/matches.out create mode 100644 test/sql/matches.sql diff --git a/src/lib.rs b/src/lib.rs index fce3a23..2eeeecd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -284,7 +284,7 @@ fn validate(id: &str, schemas: &[Value], instance: Value) -> Result Err(e), Ok(index) => { if let Err(e) = schemas.validate(&instance, index) { - info!("{e:#}"); + info!("{e}"); return Ok(false); } Ok(true) diff --git a/test/expected/is_valid.out b/test/expected/is_valid.out index 49d96c2..d6395ed 100644 --- a/test/expected/is_valid.out +++ b/test/expected/is_valid.out @@ -115,16 +115,3 @@ INFO: error loading file:///nonesuch: unsupported scheme in file:///nonesuch f (1 row) --- pg_jsonschema-compatible functions -SELECT json_matches_schema('{"type": "object"}'::json, '{"hi": "there"}'::json); - json_matches_schema ---------------------- - t -(1 row) - -SELECT jsonb_matches_schema('{"type": "object"}'::json, '{"hi": "there"}'::jsonb); - jsonb_matches_schema ----------------------- - t -(1 row) - diff --git a/test/expected/matches.out b/test/expected/matches.out new file mode 100644 index 0000000..12e5ac0 --- /dev/null +++ b/test/expected/matches.out @@ -0,0 +1,68 @@ +-- pg_jsonschema-compatible functions +-- Valid JSON +SELECT json_matches_schema('{"type": "object"}', '{"hi": "there"}'); + json_matches_schema +--------------------- + t +(1 row) + +SELECT jsonb_matches_schema('{"type": "object"}', '{"hi": "there"}'); + jsonb_matches_schema +---------------------- + t +(1 row) + +-- Invalid JSON +SELECT json_matches_schema('{"type": "object"}', '["not an object"]'); +INFO: jsonschema validation failed with file:///schema.json# +- at '': want object, but got array + json_matches_schema +--------------------- + f +(1 row) + +SELECT jsonb_matches_schema('{"type": "object"}', '["not an object"]'); +INFO: jsonschema validation failed with file:///schema.json# +- at '': want object, but got array + jsonb_matches_schema +---------------------- + f +(1 row) + +-- Invalid schema +SELECT json_matches_schema('{"type": "nonesuch"}', '{"x": "y"}'); +ERROR: file:///schema.json is not valid against metaschema: jsonschema validation failed with https://json-schema.org/draft/2020-12/schema# +- at '/type': anyOf failed + - at '/type': value must be one of 'array', 'boolean', 'integer', 'null', 'number', 'object', 'string' + - at '/type': want array, but got string +SELECT jsonb_matches_schema('{"type": "nonesuch"}', '{"x": "y"}'); +ERROR: file:///schema.json is not valid against metaschema: jsonschema validation failed with https://json-schema.org/draft/2020-12/schema# +- at '/type': anyOf failed + - at '/type': value must be one of 'array', 'boolean', 'integer', 'null', 'number', 'object', 'string' + - at '/type': want array, but got string +-- NULL instance +SELECT json_matches_schema('{"type": "object"}', NULL); + json_matches_schema +--------------------- + +(1 row) + +SELECT jsonb_matches_schema('{"type": "object"}', NULL); + jsonb_matches_schema +---------------------- + +(1 row) + +-- NULL schema +SELECT json_matches_schema(NULL, '{"x": "y"}'); + json_matches_schema +--------------------- + +(1 row) + +SELECT jsonb_matches_schema(NULL, '{"x": "y"}'); + jsonb_matches_schema +---------------------- + +(1 row) + diff --git a/test/sql/is_valid.sql b/test/sql/is_valid.sql index 8be8412..781e9d9 100644 --- a/test/sql/is_valid.sql +++ b/test/sql/is_valid.sql @@ -35,9 +35,3 @@ SELECT jsonschema_is_valid('nonesuch', '{"type": "object"}'::jsonb); -- No such ID SELECT jsonschema_is_valid('file:///nonesuch', :'addr_schema'::json); SELECT jsonschema_is_valid('file:///nonesuch', :'addr_schema'::jsonb); - - - --- pg_jsonschema-compatible functions -SELECT json_matches_schema('{"type": "object"}'::json, '{"hi": "there"}'::json); -SELECT jsonb_matches_schema('{"type": "object"}'::json, '{"hi": "there"}'::jsonb); diff --git a/test/sql/matches.sql b/test/sql/matches.sql new file mode 100644 index 0000000..e7cdcad --- /dev/null +++ b/test/sql/matches.sql @@ -0,0 +1,21 @@ +-- pg_jsonschema-compatible functions + +-- Valid JSON +SELECT json_matches_schema('{"type": "object"}', '{"hi": "there"}'); +SELECT jsonb_matches_schema('{"type": "object"}', '{"hi": "there"}'); + +-- Invalid JSON +SELECT json_matches_schema('{"type": "object"}', '["not an object"]'); +SELECT jsonb_matches_schema('{"type": "object"}', '["not an object"]'); + +-- Invalid schema +SELECT json_matches_schema('{"type": "nonesuch"}', '{"x": "y"}'); +SELECT jsonb_matches_schema('{"type": "nonesuch"}', '{"x": "y"}'); + +-- NULL instance +SELECT json_matches_schema('{"type": "object"}', NULL); +SELECT jsonb_matches_schema('{"type": "object"}', NULL); + +-- NULL schema +SELECT json_matches_schema(NULL, '{"x": "y"}'); +SELECT jsonb_matches_schema(NULL, '{"x": "y"}'); diff --git a/test/sql/validates.sql b/test/sql/validates.sql index 07c901b..8782bf9 100644 --- a/test/sql/validates.sql +++ b/test/sql/validates.sql @@ -1,3 +1,4 @@ -- pg_jsonschema-compatible functions SELECT json_matches_schema('{"type": "object"}'::json, '{"hi": "there"}'::json); SELECT jsonb_matches_schema('{"type": "object"}'::json, '{"hi": "there"}'::jsonb); +