Validate functions in the JSON Schema DuckDB extension
Function category
Validate
2 functionsConformance checks. json_schema_validate checks a value against a schema; json_schema_validate_schema checks whether a schema can be loaded. Both return TRUE on success and raise on failure. Wrap with COALESCE(TRY(...), false) when a boolean filter is required.
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
schema
|
Type
JSON
|
Mode Positional | Description The JSON Schema document to validate against. The validator's primary target is draft-07; newer drafts work for the keywords the underlying pboettch/json-schema-validator supports. |
Argument
json_data
|
Type
JSON
|
Mode Positional |
Description
The JSON value to validate. Pass a column of type JSON, a struct literal, or a string cast with ::JSON.
|
Returns
BOOLEAN — TRUE for conforming data; raises an error for nonconforming data or an invalid schema. SQL NULL inputs propagate NULL.
Description
Validate a JSON value against a JSON Schema. Validation failures raise an error; they do not return FALSE. For row filtering or valid/invalid counts, use COALESCE(TRY(json_schema_validate(schema, payload)), false). This treats SQL NULL as invalid too. Validate the schema separately first: TRY also catches malformed-schema errors. The function reports a textual exception, not a structured per-keyword report.
SELECT json_schema_validate('{
"$schema": "https://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"}
},
"required": ["id"]
}', {'id': 5, 'name': 'George'}) AS ok;
-- TRUE
INSERT INTO events_clean
SELECT * FROM events_raw
WHERE COALESCE(TRY(json_schema_validate(:schema, payload)), false);
SELECT
COUNT(*) AS total,
COUNT(*) FILTER (WHERE COALESCE(TRY(json_schema_validate(:schema, payload)), false)) AS valid,
COUNT(*) FILTER (WHERE NOT COALESCE(TRY(json_schema_validate(:schema, payload)), false)) AS invalid
FROM events;
Related functions
- json_schema_validate_schema() — Check that the validator can load a JSON Schema
- json_schema_patch() — Compute the JSON Patch (RFC 6902) diff that would bring `json_data` into conformance with the `default` values declared by the schema
- json_schema_update() — Apply the schema's `default` values inline
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
schema
|
Type
JSON
|
Mode Positional | Description Candidate JSON Schema. The function asks the validator to load it; this is not exhaustive validation against the JSON Schema meta-schema. |
Returns
BOOLEAN — TRUE if the schema is accepted; raises an error if it is invalid. SQL NULL propagates NULL.
Description
Check that the validator can load a JSON Schema. An invalid schema raises an error, making this useful as a deployment gate. Use COALESCE(TRY(json_schema_validate_schema(definition)), false) when auditing a registry and you want invalid entries flagged instead of aborting the query. Successful loading is not exhaustive meta-schema validation; some unsupported or misspelled keyword values may be accepted.
SELECT json_schema_validate_schema('{
"$schema": "https://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"id": {"type": "integer"}
}
}') AS schema_ok;
-- TRUE
SELECT name, version
FROM schema_registry
WHERE NOT COALESCE(TRY(json_schema_validate_schema(definition)), false);