
Checking a JSON Column Against a Schema in PostgreSQL
Enforce the shape of a PostgreSQL jsonb column with CHECK constraints, NOT VALID and jsonpath, including the NULL trap that lets a string id pass.
Tag
52 posts

Enforce the shape of a PostgreSQL jsonb column with CHECK constraints, NOT VALID and jsonpath, including the NULL trap that lets a string id pass.

Draft a JSON Schema from sample documents with genson, see what it infers and what it misses, and turn the draft into a contract with DuckDB as a cross-check.

Write a data contract as a YAML file, check a table against it with DuckDB, and fail the job on nulls, duplicates, bad values and drift, with tested output.

Reuse schema pieces with $ref and $defs: local pointers, references between files by $id, recursive trees, and resolver and draft traps in Python and Node.

Where Python and Node disagree on JSON Schema formats and patterns: email, uri, hostname, digits, trailing newlines, plus exact enum and const behavior.

Validate arrays of objects, tuples and deep nesting with JSON Schema: items, prefixItems, contains, uniqueItems and error paths, tested in Python.

Find YAML keys repeated inside a file and keys that differ across environment files, with ruamel.yaml, yamllint and a flatten pass over a repository.

How required, null, empty strings and defaults differ in JSON Schema, with test results for dependentRequired and if-then rules for conditional fields.

Build a JSON Schema from a user profile document one keyword at a time, run it in Python after each step, and catch typos that a validator ignores silently.

A working guide to JSON Schema: write a schema, run it in Python and Node, handle formats, refs and versions, and check JSON stored in a database.

Read a CSV in a dbt model with DuckDB and test it with unique, accepted_values, relationships and SQL tests: 6 of 8 failed on a faulty file.

What to agree with whoever sends you CSV files, as a document plus a manifest your code can check: six deliveries, six different rejections, 193,708 rows.