Skip to content

Commit

Permalink
Add jsonb?_matches_schema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Apr 12, 2024
1 parent 25b20f2 commit ce503b1
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ fn validate(id: &str, schemas: &[Value], instance: Value) -> Result<bool, Compil
Err(e) => Err(e),
Ok(index) => {
if let Err(e) = schemas.validate(&instance, index) {
info!("{e:#}");
info!("{e}");
return Ok(false);
}
Ok(true)
Expand Down
13 changes: 0 additions & 13 deletions test/expected/is_valid.out
Original file line number Diff line number Diff line change
Expand Up @@ -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)

68 changes: 68 additions & 0 deletions test/expected/matches.out
Original file line number Diff line number Diff line change
@@ -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)

6 changes: 0 additions & 6 deletions test/sql/is_valid.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
21 changes: 21 additions & 0 deletions test/sql/matches.sql
Original file line number Diff line number Diff line change
@@ -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"}');
1 change: 1 addition & 0 deletions test/sql/validates.sql
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit ce503b1

Please sign in to comment.