Skip to content

Validate functions in the JSON Schema DuckDB extension

Function category

Validate

2 functions

json_schema_validate

Scalar function Validate
Signature
json_schema_validate(schema: JSON, json_data: JSON) → BOOLEAN
Arguments (Positional)
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
Description
1 Validate a single document
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
2 Filter a column on schema conformance
INSERT INTO events_clean
SELECT * FROM events_raw
WHERE COALESCE(TRY(json_schema_validate(:schema, payload)), false);
3 Aggregate valid / invalid counts
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;

json_schema_validate_schema

Scalar function Validate
Signature
json_schema_validate_schema(schema: JSON) → BOOLEAN
Arguments (Positional)
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
Description
1 Lint a schema before deploying
SELECT json_schema_validate_schema('{
  "$schema": "https://json-schema.org/draft-07/schema",
  "type": "object",
  "properties": {
    "id": {"type": "integer"}
  }
}') AS schema_ok;
-- TRUE
2 Find any malformed schemas in a registry table
SELECT name, version
FROM schema_registry
WHERE NOT COALESCE(TRY(json_schema_validate_schema(definition)), false);