jsonschema

PostgreSQL JSON Schema 校验函数

概览

扩展包名版本分类许可证语言
jsonschema0.1.9FEATMITRust
ID扩展名BinLibLoadCreateTrustReloc模式
2760jsonschema-
相关扩展pg_jsonschema jsquery pg_graphql plv8

Distinct from Supabase pg_jsonschema; pgrx patched to 0.18.1.

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY0.1.91817161514jsonschema-
RPMPIGSTY0.1.91817161514jsonschema_$v-
DEBPIGSTY0.1.91817161514postgresql-$v-jsonschema-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
u22.x86_64
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
u22.aarch64
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
u24.x86_64
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
u24.aarch64
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
u26.x86_64
u26.aarch64
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9
PIGSTY 0.1.9

构建

您可以使用 pig build 命令构建 jsonschema 扩展的 RPM / DEB 包:

pig build pkg jsonschema         # 构建 RPM / DEB 包

安装

您可以直接安装 jsonschema 扩展包的预置二进制包,首先确保 PGDGPIGSTY 仓库已经添加并启用:

pig repo add pgsql -u          # 添加仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install jsonschema;          # 当前活跃 PG 版本安装
pig ext install -y jsonschema -v 18  # PG 18
pig ext install -y jsonschema -v 17  # PG 17
pig ext install -y jsonschema -v 16  # PG 16
pig ext install -y jsonschema -v 15  # PG 15
pig ext install -y jsonschema -v 14  # PG 14
dnf install -y jsonschema_18       # PG 18
dnf install -y jsonschema_17       # PG 17
dnf install -y jsonschema_16       # PG 16
dnf install -y jsonschema_15       # PG 15
dnf install -y jsonschema_14       # PG 14
apt install -y postgresql-18-jsonschema   # PG 18
apt install -y postgresql-17-jsonschema   # PG 17
apt install -y postgresql-16-jsonschema   # PG 16
apt install -y postgresql-15-jsonschema   # PG 15
apt install -y postgresql-14-jsonschema   # PG 14

创建扩展

CREATE EXTENSION jsonschema;

来源:jsonschema v0.1.9 READMEdocumentationcontrol fileSQL definition

用法

jsonschema 在 PostgreSQL 内根据 JSON Schema 校验 JSON 和 JSONB 值。它是 theory/pg-jsonschema-boon 扩展,不同于 Supabase pg_jsonschema,但提供了名为 json_matches_schema()jsonb_matches_schema() 的兼容包装函数。

该扩展通过 Rust boon 校验器支持 JSON Schema draft 4、draft 6、draft 7、draft 2019-09 和 draft 2020-12。运行时除了 PostgreSQL 之外没有其他依赖。

校验 Schema 与文档

CREATE EXTENSION IF NOT EXISTS jsonschema;

SELECT jsonschema_is_valid(
  '{
     "type": "object",
     "required": ["name", "email"],
     "properties": {
       "name":  { "type": "string" },
       "age":   { "type": "number", "minimum": 0 },
       "email": { "type": "string", "format": "email" }
     }
   }'::json
);

SELECT jsonschema_validates(
  '{"name":"Amos Burton","email":"amos@rocinante.ship"}'::json,
  '{
     "type": "object",
     "required": ["name", "email"],
     "properties": {
       "name":  { "type": "string" },
       "email": { "type": "string", "format": "email" }
     }
   }'::json
);

jsonschema_is_valid(schema) 返回 schema 本身是否能够编译,并能按所选 draft 通过校验。jsonschema_validates(data, schema) 返回 JSON/JSONB 值是否满足该 schema。

CHECK 约束

CREATE TABLE customer_profile (
  id       bigserial PRIMARY KEY,
  profile  jsonb NOT NULL,
  CHECK (
    jsonschema_validates(
      profile,
      '{
         "type": "object",
         "required": ["email"],
         "properties": {
           "email": { "type": "string", "format": "email" },
           "tags":  {
             "type": "array",
             "items": { "type": "string", "maxLength": 16 }
           }
         }
       }'::jsonb
    )
  )
);

当数据库需要在写入时拒绝格式错误的 JSON 文档时,可使用约束。

组合式 Schema

SELECT jsonschema_validates(
  jsonb_build_object(
    'first_name', 'Naomi',
    'last_name', 'Nagata',
    'shipping_address', jsonb_build_object(
      'street_address', '1 Rocinante Way',
      'city', 'Ceres Station',
      'state', 'The Belt'
    )
  ),
  'https://example.com/schemas/customer',
  '{
     "$id": "https://example.com/schemas/address",
     "type": "object",
     "required": ["street_address", "city", "state"],
     "properties": {
       "street_address": { "type": "string" },
       "city": { "type": "string" },
       "state": { "type": "string" }
     }
   }'::jsonb,
  '{
     "$id": "https://example.com/schemas/customer",
     "type": "object",
     "required": ["first_name", "last_name", "shipping_address"],
     "properties": {
       "first_name": { "type": "string" },
       "last_name": { "type": "string" },
       "shipping_address": { "$ref": "/schemas/address" }
     }
   }'::jsonb
);

id 的重载允许多个 schema 通过 $id 相互引用,适合组件化的 JSON Schema 定义。

兼容函数

SELECT json_matches_schema(
  '{"type":"string","maxLength":4}'::json,
  '"1234"'::json
);

SELECT jsonb_matches_schema(
  '{"type":"object","required":["id"]}'::json,
  '{"id":42}'::jsonb
);

这些包装函数沿用常见的 pg_jsonschema 参数顺序:schema 在前,instance 在后。

Draft 版本选择与注意事项

SET jsonschema.default_draft = 'V2020';
SET jsonschema.default_draft = 'V7';

如果 schema 省略 $schemajsonschema.default_draft 会控制默认 draft。支持值为 V4V6V7V2019V2020

  • 如果任一参数为 NULL,jsonschema_validates(data, schema) 返回 NULL。
  • 无效或无法编译的 schema 可能在校验调用中抛出错误;文档校验失败会返回 false,并在 INFO 级别记录详情。
  • jsonschema_is_valid(id, VARIADIC schemas)jsonschema_validates(data, id, VARIADIC schemas) 需要匹配的 $id 值,才能可靠解析组合式 schema。

最后修改 2026-06-18: extension data update (63e2bd9)