The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
postgres extension: pg_jsonschema doc
Oliver Rice23bf8b9f8279
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.tsindex aa25744b65f..e939cafa4b4 100644--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts@@ -409,6 +409,11 @@ export const database = {url: '/guides/database/extensions/pgcron',items: [],},+ {+ name: 'pg_jsonschema: JSON Schema Validation',+ url: '/guides/database/extensions/pg_jsonschema',+ items: [],+ },{name: 'pg_net: Async Networking',url: '/guides/database/extensions/pgnet',diff --git a/apps/docs/pages/guides/database/extensions/pg_jsonschema.mdx b/apps/docs/pages/guides/database/extensions/pg_jsonschema.mdxnew file mode 100644index 00000000000..bfe71f75a46--- /dev/null+++ b/apps/docs/pages/guides/database/extensions/pg_jsonschema.mdx@@ -0,0 +1,114 @@+import Layout from '~/layouts/DefaultGuideLayout'++export const meta = {+ id: 'pg_jsonschema',+ title: 'pg_jsonschema: JSON Schema Validation',+ description: 'Validate json/jsonb with JSON Schema in PostgreSQL.',+}++[`pg_jsonschema`](https://json-schema.org) is a PostgreSQL extension adding support for JSON schema validation on json and jsonb data types.++## Overview++[JSON Schema](https://json-schema.org) is a language for annotating and validating JSON documents. `pg_jsonschema` adds the ability to validate PostgreSQL's builtin `json` and `jsonb` data types against a JSON Schema document.++## Usage++### Enable the extension++<Tabs+ scrollable+ size="small"+ type="underlined"+ defaultActiveId="dashboard"+>+<TabPanel id="dashboard" label="Dashboard">++1. Go to the [Database](https://app.supabase.com/project/_/database/tables) page in the Dashboard.+2. Click on **Extensions** in the sidebar.+3. Search for "pg_jsonschema" and enable the extension.++</TabPanel>+<TabPanel id="sql" label="SQL">++```sql+-- Enable the "pg_jsonschema" extension+create extension pg_jsonschema with schema extensions;++-- Disable the "pg_jsonschema" extension+drop extension if exists pg_jsonschema;+```++Even though the SQL code is `create extension`, this is the equivalent of "enabling the extension".+To disable an extension you can call `drop extension`.++It's good practice to create the extension within a separate schema (like `extensions`) to keep your database clean.++</TabPanel>+</Tabs>++API:++- [`json_matches_schema(schema json, instance json)`](https://github.com/supabase/pg_jsonschema#api): Checks if a `json` *instance* conforms to a JSON Schema *schema*.+- [`jsonb_matches_schema(schema json, instance jsonb)`](https://github.com/supabase/pg_jsonschema#api): Checks if a `jsonb` *instance* conforms to a JSON Schema *schema*.++### Validating a Document++Since `pg_jsonschema` exposes its utilities as functions, we can test it with a simple select statement++```sql+select+ extensions.json_matches_schema(+ schema := '{"type": "object"}',+ instance := '{}'+ );+```++`pg_jsonschema` is generally used in tandem with a [check constraint](https://www.postgresql.org/docs/current/ddl-constraints.html) as a way to constrain the contents of a json/b field to match a JSON Schema.++```sql+create table customer(+ id serial primary key,+ ...+ metadata json,++ check (+ json_matches_schema(+ '{+ "type": "object",+ "properties": {+ "tags": {+ "type": "array",+ "items": {+ "type": "string",+ "maxLength": 16+ }+ }+ }+ }',+ metadata+ )+ )+);++-- Example: Valid Payload+insert into customer(metadata)+values ('{"tags": ["vip", "darkmode-ui"]}');+-- Result:+-- INSERT 0 1++-- Example: Invalid Payload+insert into customer(metadata)+values ('{"tags": [1, 3]}');+-- Result:+-- ERROR: new row for relation "customer" violates check constraint "customer_metadata_check"+-- DETAIL: Failing row contains (2, {"tags": [1, 3]}).+```++## Resources++- Official [`pg_jsonschema` documentation](https://github.com/supabase/pg_jsonschema)++export const Page = ({ children }) => <Layout meta={meta} children={children} />++export default Page