Skip to content

Commit df22f0f

Browse files
committed
updated readme
1 parent 66b7728 commit df22f0f

File tree

1 file changed

+79
-8
lines changed

1 file changed

+79
-8
lines changed

Diff for: README.md

+79-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ schemas](http://json-schema.org/) in PostgreSQL. It is implemented as a
77
PL/pgSQL function and you can use it as a check constraint to validate the
88
format of your JSON columns.
99

10-
postgres-json-schema supports the entire JSON schema draft v4 spec, except for
10+
postgres-json-schema supports the entire JSON schema draft v4 and v7 spec, except for
1111
remote (http) references. It's tested against the official
1212
[JSON-Schema-Test-Suite](https://github.com/json-schema-org/JSON-Schema-Test-Suite).
1313

@@ -17,14 +17,85 @@ postgres-json-schema is packaged as an PGXS extension. To install, just run
1717
`make install` as root, then `CREATE EXTENSION "postgres-json-schema";` as the
1818
database superuser.
1919

20+
# Usage
21+
22+
## Types
23+
- `json_schema_validation_result` A composite type which contains error messages and path (an array to the path) within json data where the validation failed
24+
#### contains the following attributes
25+
- `path` a `text[]` to the path where the validation failed
26+
- `error` the validation error message
27+
28+
## Functions
29+
30+
#### Functions accepting a argument `string_as_number` controlling whether a schema expecting a number may contain a valid number as a string. This is useful when dealing with for example python Decimal, which most implementations serialize it to json as a quoted string not to lose decimal precision.
31+
32+
- ```sql
33+
-- Returns bool
34+
validate_json_schema(schema jsonb, data jsonb, string_as_number bool)
35+
```
36+
- ```sql
37+
-- Returns ARRAY json_schema_validation_result[]
38+
get_json_schema_validations(schema jsonb, data jsonb, string_as_number bool)
39+
```
40+
- ```sql
41+
-- Returns true if valid,
42+
-- otherwise raises a check_constraint exception, this is useful when you want to get a detailed
43+
-- error knowing which part of the json document failed to validate.
44+
json_schema_check_constraint(
45+
schema jsonb,
46+
data jsonb,
47+
string_as_number bool default false,
48+
table_name text default '', -- if you need to set the value for TABLE in the PG_EXCEPTION_CONTEXT
49+
column_name text default '' -- if you need to set the value for COLUMN in the PG_EXCEPTION_CONTEXT
50+
)
51+
```
52+
- `json_schema_resolve_refs( schema )`
53+
54+
When dealing with a JSON schema that has `$id` uri values being used in `$ref`,
55+
there is a convenient function to resolve those references
56+
```sql
57+
validate_json_schema( json_schema_resolve_refs( schema ), data );
58+
-- or
59+
json_schema_check_constraint( json_schema_resolve_refs( schema ), data, ... );
60+
```
61+
62+
2063
# Example
2164

22-
CREATE TABLE example (id serial PRIMARY KEY, data jsonb);
23-
ALTER TABLE example ADD CONSTRAINT data_is_valid CHECK (validate_json_schema('{"type": "object"}', data));
65+
#### Using standard default check constraint with boolean function
66+
```sql
67+
CREATE TABLE example (id serial PRIMARY KEY, data jsonb);
68+
ALTER TABLE example ADD CONSTRAINT data_is_valid CHECK (validate_json_schema('{"type": "object"}', data));
69+
70+
INSERT INTO example (data) VALUES ('{}');
71+
-- INSERT 0 1
72+
73+
INSERT INTO example (data) VALUES ('1');
74+
-- ERROR: new row for relation "example" violates check constraint "data_is_valid"
75+
-- DETAIL: Failing row contains (2, 1).
76+
```
77+
78+
79+
#### Using custom check constraint exception with detailed error
80+
```sql
81+
CREATE TABLE example (id serial PRIMARY KEY, data jsonb);
82+
ALTER TABLE example ADD CONSTRAINT data_is_valid CHECK (json_schema_check_constraint('{"type": "object", "properties": { "foo": {"type": "number"}, "bar": { "prefixItems": [{ "type": "number" }, { "type": "number", "minimum": 2 }] } }}', data, true, table_name := 'example', column_name := 'data'));
83+
84+
INSERT INTO example (data) VALUES ('{}');
85+
-- INSERT 0 1
86+
87+
INSERT INTO example (data) VALUES ('1');
88+
-- ERROR: json_schema_validation_failed
89+
-- DETAIL: [{"path": [], "error": "number is not a valid type: {object}"}]
90+
-- CONTEXT: PL/pgSQL function json_schema_check_constraint(jsonb,jsonb,boolean,text,text) line 7 at RAISE
2491

25-
INSERT INTO example (data) VALUES ('{}');
26-
-- INSERT 0 1
92+
INSERT INTO example (data) VALUES ('{ "foo": "string" }');
93+
-- ERROR: json_schema_validation_failed
94+
-- DETAIL: [{"path": ["foo"], "error": "string is not a valid type: {number}"}]
95+
-- CONTEXT: PL/pgSQL function json_schema_check_constraint(jsonb,jsonb,boolean,text,text) line 7 at RAISE
2796

28-
INSERT INTO example (data) VALUES ('1');
29-
-- ERROR: new row for relation "example" violates check constraint "data_is_valid"
30-
-- DETAIL: Failing row contains (2, 1).
97+
INSERT INTO example (data) VALUES ('{ "foo": 1, "bar": ["a", 1.1] }');
98+
-- ERROR: json_schema_validation_failed
99+
-- DETAIL: [{"path": ["bar", "0"], "error": "string is not a valid type: {number}"}, {"path": ["bar", "1"], "error": "value must be >= 2"}]
100+
-- CONTEXT: PL/pgSQL function json_schema_check_constraint(jsonb,jsonb,boolean,text,text) line 7 at RAISE
101+
```

0 commit comments

Comments
 (0)