74 lines
1.5 KiB
Plaintext
74 lines
1.5 KiB
Plaintext
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;
|