PostgreSQL extension providing JSON Schema validation
PostgreSQL extension providing JSON Schema validation
Source Code: https://github.com/supabase/pg_jsonschema
pg_jsonschema is a PostgreSQL extension adding support for JSON schema validation on json and jsonb data types.
This extension exposes the following four SQL functions:
With the following signatures
-- Validates a json *instance* against a *schema*
json_matches_schema(schema json, instance json) returns booland
-- Validates a jsonb *instance* against a *schema*
jsonb_matches_schema(schema json, instance jsonb) returns booland
-- Validates whether a json *schema* is valid
jsonschema_is_valid(schema json) returns booland
-- Returns an array of errors if a *schema* is invalid
jsonschema_validation_errors(schema json, instance json) returns text[]For repeated validation against the same schema, cast it to jsonschema once. The validator is compiled and cached per callsite, avoiding recompilation on every row.
-- Validates a json instance against a pre-compiled schema
json_matches_compiled_schema(schema jsonschema, instance json) returns bool
-- Validates a jsonb instance against a pre-compiled schema
jsonb_matches_compiled_schema(schema jsonschema, instance jsonb) returns bool
-- Returns validation errors for a json instance
jsonschema_validation_errors_compiled(schema jsonschema, instance json) returns text[]
-- Returns validation errors for a jsonb instance
jsonb_validation_errors_compiled(schema jsonschema, instance jsonb) returns text[]Those functions can be used to constrain json and jsonb columns to conform to a schema.
For example:
…pg_jsonschema is a (very) thin wrapper around the jsonschema rust crate. Visit their docs for full details on which drafts of the JSON Schema spec are supported.
Spin up Postgres with pg_jsonschema installed in a docker container via docker-compose up. The database is available at postgresql://postgres:password@localhost:5407/app
Requires:
cargo pgrx runwhich drops into a psql prompt.
psql (13.6)
Type "help" for help.
pg_jsonschema=# create extension pg_jsonschema;
CREATE EXTENSION
pg_jsonschema=# select json_matches_schema('{"type": "object"}', '{}');
json_matches_schema
---------------------
t
(1 row)for more complete installation guidelines see the pgrx docs.
Releases are automated via a single command:
./scripts/release.sh For example:
./scripts/release.sh 0.4.0This orchestrates the full release process end-to-end:
Cargo.toml, META.json, and Cargo.lock; creates a release/ branch; commits, pushes, and waits for the PR to be merged into masterv tag, which triggers the CI workflows (release.yml for GitHub release + .deb artifacts, pgxn-release.yml for PGXN)Note:
pg_jsonschema.controluses@CARGO_VERSION@which is substituted by pgrx at build time fromCargo.toml, so it doesn't need manual updates.
The script is safe to re-run if interrupted — it detects what has already been completed (branch exists, tag exists, release exists) and picks up where it left off.
The release process is composed of smaller scripts that can also be run independently:
| Script | Purpose |
|---|---|
scripts/check-version.sh |
Checks if Cargo.toml and META.json match the given version |
scripts/update-version.sh |
Updates version files, creates a release branch, and waits for PR merge |
scripts/update-version.sh --files-only |
Updates version files without any git operations |
scripts/push-tag.sh |
Creates and pushes the git tag, then monitors for the GitHub release |
scripts/push-tag.sh --dry-run |
Validates versions without creating a tag |
postgres-json-schema - JSON Schema Postgres extension written in PL/pgSQL
is_jsonb_valid - JSON Schema Postgres extension written in C
pgx_json_schema - JSON Schema Postgres extension written with pgrx + jsonschema
Validating the following schema on 20k unique inserts
{
"type": "object",
"properties": {
"a": { "type": "number" },
"b": { "type": "string" }
}
}create table bench_test_pg_jsonschema(
meta jsonb,
check (
jsonb_matches_schema(
'{"type": "object", "properties": {"a": {"type": "number"}, "b": {"type": "string"}}}',
meta
)
)
);
insert into bench_test_pg_jsonschema(meta)
select
json_build_object(
'a', i,
'b', i::text
)
from
generate_series(1, 20000) t(i);
-- Query Completed in 195 msfor comparison, the equivalent test using postgres-json-schema's validate_json_schema function ran in 2.0 seconds. pg_jsonschema's ~10x speedup on this example JSON schema grows quickly as the schema becomes more complex.
Using the same schema and 20k inserts:
| Method | 20k inserts |
|---|---|
jsonb_matches_schema — recompiles every row |
~195 ms |
jsonb_matches_compiled_schema — compiled once, cached |
~110 ms |
~1.8x speedup; the gain grows with schema complexity since compilation cost is paid only once per callsite.
No open issues yet, or sync has not completed.