Skip to content

gavinwahl/postgres-json-schema

Folders and files

NameName
Last commit message
Last commit date

Latest commit

e25c758 · Oct 10, 2023

History

13 Commits
Oct 8, 2016
Oct 8, 2016
Jul 16, 2018
Jan 5, 2017
Aug 8, 2019
Nov 18, 2017
Oct 10, 2023
Aug 8, 2019
Nov 18, 2017
Dec 22, 2016

Repository files navigation

postgres-json-schema

Build Status

postgres-json-schema allows validation of JSON schemas in PostgreSQL. It is implemented as a PL/pgSQL function and you can use it as a check constraint to validate the format of your JSON columns.

postgres-json-schema supports the entire JSON schema draft v4 spec, except for remote (http) references. It's tested against the official JSON-Schema-Test-Suite.

Installation

postgres-json-schema is packaged as an PGXS extension. To install, just run make install as root, then CREATE EXTENSION "postgres-json-schema"; as the database superuser.

Example

CREATE TABLE example (id serial PRIMARY KEY, data jsonb);
ALTER TABLE example ADD CONSTRAINT data_is_valid CHECK (validate_json_schema('{"type": "object"}', data));

INSERT INTO example (data) VALUES ('{}');
-- INSERT 0 1

INSERT INTO example (data) VALUES ('1');
-- ERROR:  new row for relation "example" violates check constraint "data_is_valid"
-- DETAIL:  Failing row contains (2, 1).