Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions nix/tests/expected/pg_jsonschema.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
begin;
-- Test json_matches_schema
create table customer(
id serial primary key,
metadata json,
check (
json_matches_schema(
'{
"type": "object",
"properties": {
"tags": {
"type": "array",
"items": {
"type": "string",
"maxLength": 16
}
}
}
}',
metadata
)
)
);
insert into customer(metadata)
values ('{"tags": ["vip", "darkmode-ui"]}');
-- Test jsonb_matches_schema
select
jsonb_matches_schema(
'{
"type": "object",
"properties": {
"tags": {
"type": "array",
"items": {
"type": "string",
"maxLength": 16
}
}
}
}',
'{"tags": ["vip", "darkmode-ui"]}'::jsonb
);
jsonb_matches_schema
----------------------
t
(1 row)

-- Test jsonschema_is_valid
select
jsonschema_is_valid(
'{
"type": "object",
"properties": {
"tags": {
"type": "array",
"items": {
"type": "string",
"maxLength": 16
}
}
}
}');
jsonschema_is_valid
---------------------
t
(1 row)

-- Test invalid payload
insert into customer(metadata)
values ('{"tags": [1, 3]}');
ERROR: new row for relation "customer" violates check constraint "customer_metadata_check"
DETAIL: Failing row contains (2, {"tags": [1, 3]}).
rollback;
68 changes: 68 additions & 0 deletions nix/tests/sql/pg_jsonschema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
begin;

-- Test json_matches_schema
create table customer(
id serial primary key,
metadata json,

check (
json_matches_schema(
'{
"type": "object",
"properties": {
"tags": {
"type": "array",
"items": {
"type": "string",
"maxLength": 16
}
}
}
}',
metadata
)
)
);

insert into customer(metadata)
values ('{"tags": ["vip", "darkmode-ui"]}');

-- Test jsonb_matches_schema
select
jsonb_matches_schema(
'{
"type": "object",
"properties": {
"tags": {
"type": "array",
"items": {
"type": "string",
"maxLength": 16
}
}
}
}',
'{"tags": ["vip", "darkmode-ui"]}'::jsonb
);

-- Test jsonschema_is_valid
select
jsonschema_is_valid(
'{
"type": "object",
"properties": {
"tags": {
"type": "array",
"items": {
"type": "string",
"maxLength": 16
}
}
}
}');

-- Test invalid payload
insert into customer(metadata)
values ('{"tags": [1, 3]}');

rollback;
Loading