-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
And move the test/demo schemas into the eg directory and add a README.
- Loading branch information
Showing
7 changed files
with
221 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
\timing off | ||
|
||
|
||
BEGIN; | ||
|
||
CREATE EXTENSION IF NOT EXISTS jsonschema; | ||
|
||
SELECT ' | ||
\ir schemas/address.schema.json | ||
' AS address_schema \gset | ||
|
||
\echo :address_schema | ||
|
||
|
||
ROLLBACK; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
jsonschema Examples | ||
=================== | ||
|
||
This directory contains example use cases for the PostgreSQL jsonschema | ||
extension. | ||
|
||
Schemas | ||
------- | ||
|
||
The following schema files are used in one or more example scripts (detailed | ||
below) and in the unit test suite (run by `make test`). | ||
|
||
### `address.schema.json` | ||
|
||
A sample JSON schema for validating mail addresses. Borrowed from the [Address | ||
Example]. | ||
|
||
### `user-profile.schema.json` | ||
|
||
A sample JSON schema for validating user profiles. Borrowed from the [User | ||
Profile Example], but augmented with an `address` property that references | ||
`address.schema.json`. | ||
|
||
Use Cases | ||
--------- | ||
|
||
### `bench.sql` | ||
|
||
This file contains a simple benchmark SQL script that demonstrates the | ||
performance of jsonschema vs [pg_jsonschema]. It requires that each be built | ||
and installed. To test jsonschema, run: | ||
|
||
```sh | ||
psql -Xf eg/bench.sql | ||
``` | ||
|
||
And to test [pg_jsonschema]: | ||
|
||
```sh | ||
psql -Xf eg/bench.sql --set extension=pg_jsonschema | ||
``` | ||
|
||
Set `iterations` to change the number of iterations (defaults to 200,000): | ||
|
||
```sh | ||
psql -Xf eg/bench.sql --set iterations=500_000 | ||
``` | ||
|
||
### `user.sql` | ||
|
||
This SQL script demonstrates the use of composed schemas to validate records | ||
in a table. It uses [jq] to load [address.schema.json`](#addressschemajson) | ||
and [user-profile.schema.json](#user-profileschemajson) into a table, | ||
validates them both, then constructs a function that uses them to validate a | ||
user profile. It then creates a table for users with a check constraint using | ||
that function. It demonstrates inserting valid and invalid user objects. | ||
|
||
The script must be run from the project root directory so that it can find and | ||
load the schema files. Run it like so: | ||
|
||
``` sh | ||
psql -Xf eg/user.sql | ||
``` | ||
|
||
[Address Example]: https://json-schema.org/learn/json-schema-examples#address | ||
[User Profile Example]: https://json-schema.org/learn/json-schema-examples#user-profile | ||
[pg_jsonschema]: https://github.com/supabase/pg_jsonschema | ||
[jq]: https://jqlang.github.io/jq/manual/ |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
\set QUIET | ||
\pset pager off | ||
SET client_min_messages TO WARNING; | ||
\set ECHO queries | ||
|
||
\unset QUIET | ||
|
||
CREATE EXTENSION jsonschema; | ||
\echo | ||
\prompt xxx | ||
|
||
SET jsonschema.default_draft TO 'V2020'; | ||
\echo | ||
\prompt xxx | ||
|
||
CREATE TEMPORARY TABLE json_schemas( | ||
schema JSON | ||
); | ||
\echo | ||
\prompt xxx | ||
|
||
\set ECHO ALL | ||
\copy json_schemas FROM PROGRAM 'jq -c . eg/*.schema.json'; | ||
\set ECHO queries | ||
\echo | ||
\prompt xxx | ||
|
||
SELECT jsonschema_is_valid( | ||
'https://example.com/user-profile.schema.json', | ||
VARIADIC ARRAY(SELECT schema from json_schemas) | ||
); | ||
\prompt xxx | ||
|
||
SELECT jsonschema_is_valid( | ||
'https://example.com/address.schema.json', | ||
VARIADIC ARRAY(SELECT schema from json_schemas) | ||
); | ||
\prompt xxx | ||
|
||
CREATE OR REPLACE FUNCTION validate_user( | ||
data json | ||
) RETURNS BOOLEAN LANGUAGE SQL STABLE AS $$ | ||
SELECT jsonschema_validates( | ||
data, 'https://example.com/user-profile.schema.json', | ||
VARIADIC ARRAY(SELECT schema from json_schemas) | ||
); | ||
$$; | ||
\echo | ||
\prompt xxx | ||
|
||
CREATE TEMPORARY TABLE json_users ( | ||
id SERIAL PRIMARY KEY, | ||
body JSON CHECK (validate_user(body)) | ||
); | ||
\echo | ||
\prompt xxx | ||
|
||
INSERT INTO json_users (body) VALUES(json_build_object( | ||
'username', 'theory', | ||
'email', '[email protected]' | ||
)); | ||
\echo | ||
\prompt xxx | ||
|
||
SELECT body FROM json_users WHERE body->>'username' = 'theory'; | ||
\prompt xxx | ||
|
||
INSERT INTO json_users (body) VALUES(json_build_object( | ||
'username', 'naomi', | ||
'email', '[email protected]', | ||
'address', json_build_object( | ||
'locality', 'Series', | ||
'region', 'The Belt', | ||
'countryName', 'Sol System' | ||
) | ||
)); | ||
\echo | ||
\prompt xxx | ||
|
||
SELECT body FROM json_users WHERE body->>'username' = 'naomi'; | ||
\prompt xxx | ||
|
||
INSERT INTO json_users (body) VALUES(json_build_object( | ||
'username', 42, | ||
'email', '[email protected]' | ||
)); | ||
\echo | ||
\prompt xxx | ||
|
||
\set ECHO none | ||
\set QUIET | ||
DROP FUNCTION validate_user(json) CASCADE; | ||
DROP EXTENSION jsonschema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters