跳转到主要内容

jsonschema

PostgreSQL JSON Schema 校验函数

jsonschema · PostgreSQL JSON Schema 校验函数

概览

ID扩展软件包版本类别许可证编程语言
2760jsonschemajsonschema0.1.11FEATMITRust
属性可执行文件动态库需要预加载需要建扩展可迁移可信
--s-d-r否是否是是否
Distinct from Supabase pg_jsonschema; upstream PG19/pgrx 0.19.3.

软件包

类型仓库版本兼容的 PostgreSQL 大版本包名模式依赖
EXTPIGSTY0.1.1119 18 17 16 15jsonschema-
RPMPIGSTY0.1.1119 18 17 16 15jsonschema_$v-
DEBPIGSTY0.1.1119 18 17 16 15postgresql-$v-jsonschema-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
el8.aarch64N/APIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
el9.x86_64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
el9.aarch64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
el10.x86_64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
el10.aarch64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
d12.x86_64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
d12.aarch64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
d13.x86_64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
d13.aarch64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
u22.x86_64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
u22.aarch64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
u24.x86_64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
u24.aarch64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
u26.x86_64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
u26.aarch64PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11PIGSTY 0.1.11
软件包版本平台仓库大小下载
jsonschema_190.1.11el9.x86_64pigsty1.9 MiBjsonschema_19-0.1.11-2PGSTY.el9.x86_64.rpm
jsonschema_190.1.11el9.aarch64pigsty1.8 MiBjsonschema_19-0.1.11-2PGSTY.el9.aarch64.rpm
jsonschema_190.1.11el10.x86_64pigsty1.9 MiBjsonschema_19-0.1.11-2PGSTY.el10.x86_64.rpm
jsonschema_190.1.11el10.aarch64pigsty1.9 MiBjsonschema_19-0.1.11-2PGSTY.el10.aarch64.rpm
postgresql-19-jsonschema0.1.11d12.x86_64pigsty1.3 MiBpostgresql-19-jsonschema_0.1.11-2PGSTY~bookworm_amd64.deb
postgresql-19-jsonschema0.1.11d12.aarch64pigsty1.1 MiBpostgresql-19-jsonschema_0.1.11-2PGSTY~bookworm_arm64.deb
postgresql-19-jsonschema0.1.11d13.x86_64pigsty1.3 MiBpostgresql-19-jsonschema_0.1.11-2PGSTY~trixie_amd64.deb
postgresql-19-jsonschema0.1.11d13.aarch64pigsty1.1 MiBpostgresql-19-jsonschema_0.1.11-2PGSTY~trixie_arm64.deb
postgresql-19-jsonschema0.1.11u22.x86_64pigsty1.4 MiBpostgresql-19-jsonschema_0.1.11-2PGSTY~jammy_amd64.deb
postgresql-19-jsonschema0.1.11u22.aarch64pigsty1.3 MiBpostgresql-19-jsonschema_0.1.11-2PGSTY~jammy_arm64.deb
postgresql-19-jsonschema0.1.11u24.x86_64pigsty1.4 MiBpostgresql-19-jsonschema_0.1.11-2PGSTY~noble_amd64.deb
postgresql-19-jsonschema0.1.11u24.aarch64pigsty1.3 MiBpostgresql-19-jsonschema_0.1.11-2PGSTY~noble_arm64.deb
postgresql-19-jsonschema0.1.11u26.x86_64pigsty1.4 MiBpostgresql-19-jsonschema_0.1.11-2PGSTY~resolute_amd64.deb
postgresql-19-jsonschema0.1.11u26.aarch64pigsty1.3 MiBpostgresql-19-jsonschema_0.1.11-2PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
jsonschema_180.1.11el8.x86_64pigsty1.7 MiBjsonschema_18-0.1.11-1PGSTY.el8.x86_64.rpm
jsonschema_180.1.11el8.aarch64pigsty1.5 MiBjsonschema_18-0.1.11-1PGSTY.el8.aarch64.rpm
jsonschema_180.1.11el9.x86_64pigsty1.9 MiBjsonschema_18-0.1.11-2PGSTY.el9.x86_64.rpm
jsonschema_180.1.11el9.aarch64pigsty1.8 MiBjsonschema_18-0.1.11-2PGSTY.el9.aarch64.rpm
jsonschema_180.1.11el10.x86_64pigsty1.9 MiBjsonschema_18-0.1.11-2PGSTY.el10.x86_64.rpm
jsonschema_180.1.11el10.aarch64pigsty1.8 MiBjsonschema_18-0.1.11-2PGSTY.el10.aarch64.rpm
postgresql-18-jsonschema0.1.11d12.x86_64pigsty1.3 MiBpostgresql-18-jsonschema_0.1.11-2PGSTY~bookworm_amd64.deb
postgresql-18-jsonschema0.1.11d12.aarch64pigsty1.1 MiBpostgresql-18-jsonschema_0.1.11-2PGSTY~bookworm_arm64.deb
postgresql-18-jsonschema0.1.11d13.x86_64pigsty1.3 MiBpostgresql-18-jsonschema_0.1.11-2PGSTY~trixie_amd64.deb
postgresql-18-jsonschema0.1.11d13.aarch64pigsty1.1 MiBpostgresql-18-jsonschema_0.1.11-2PGSTY~trixie_arm64.deb
postgresql-18-jsonschema0.1.11u22.x86_64pigsty1.4 MiBpostgresql-18-jsonschema_0.1.11-2PGSTY~jammy_amd64.deb
postgresql-18-jsonschema0.1.11u22.aarch64pigsty1.3 MiBpostgresql-18-jsonschema_0.1.11-2PGSTY~jammy_arm64.deb
postgresql-18-jsonschema0.1.11u24.x86_64pigsty1.4 MiBpostgresql-18-jsonschema_0.1.11-2PGSTY~noble_amd64.deb
postgresql-18-jsonschema0.1.11u24.aarch64pigsty1.3 MiBpostgresql-18-jsonschema_0.1.11-2PGSTY~noble_arm64.deb
postgresql-18-jsonschema0.1.11u26.x86_64pigsty1.4 MiBpostgresql-18-jsonschema_0.1.11-2PGSTY~resolute_amd64.deb
postgresql-18-jsonschema0.1.11u26.aarch64pigsty1.3 MiBpostgresql-18-jsonschema_0.1.11-2PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
jsonschema_170.1.11el8.x86_64pigsty1.7 MiBjsonschema_17-0.1.11-1PGSTY.el8.x86_64.rpm
jsonschema_170.1.11el8.aarch64pigsty1.5 MiBjsonschema_17-0.1.11-1PGSTY.el8.aarch64.rpm
jsonschema_170.1.11el9.x86_64pigsty1.9 MiBjsonschema_17-0.1.11-2PGSTY.el9.x86_64.rpm
jsonschema_170.1.11el9.aarch64pigsty1.8 MiBjsonschema_17-0.1.11-2PGSTY.el9.aarch64.rpm
jsonschema_170.1.11el10.x86_64pigsty1.9 MiBjsonschema_17-0.1.11-2PGSTY.el10.x86_64.rpm
jsonschema_170.1.11el10.aarch64pigsty1.8 MiBjsonschema_17-0.1.11-2PGSTY.el10.aarch64.rpm
postgresql-17-jsonschema0.1.11d12.x86_64pigsty1.3 MiBpostgresql-17-jsonschema_0.1.11-2PGSTY~bookworm_amd64.deb
postgresql-17-jsonschema0.1.11d12.aarch64pigsty1.1 MiBpostgresql-17-jsonschema_0.1.11-2PGSTY~bookworm_arm64.deb
postgresql-17-jsonschema0.1.11d13.x86_64pigsty1.3 MiBpostgresql-17-jsonschema_0.1.11-2PGSTY~trixie_amd64.deb
postgresql-17-jsonschema0.1.11d13.aarch64pigsty1.1 MiBpostgresql-17-jsonschema_0.1.11-2PGSTY~trixie_arm64.deb
postgresql-17-jsonschema0.1.11u22.x86_64pigsty1.4 MiBpostgresql-17-jsonschema_0.1.11-2PGSTY~jammy_amd64.deb
postgresql-17-jsonschema0.1.11u22.aarch64pigsty1.3 MiBpostgresql-17-jsonschema_0.1.11-2PGSTY~jammy_arm64.deb
postgresql-17-jsonschema0.1.11u24.x86_64pigsty1.4 MiBpostgresql-17-jsonschema_0.1.11-2PGSTY~noble_amd64.deb
postgresql-17-jsonschema0.1.11u24.aarch64pigsty1.3 MiBpostgresql-17-jsonschema_0.1.11-2PGSTY~noble_arm64.deb
postgresql-17-jsonschema0.1.11u26.x86_64pigsty1.4 MiBpostgresql-17-jsonschema_0.1.11-2PGSTY~resolute_amd64.deb
postgresql-17-jsonschema0.1.11u26.aarch64pigsty1.3 MiBpostgresql-17-jsonschema_0.1.11-2PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
jsonschema_160.1.11el8.x86_64pigsty1.6 MiBjsonschema_16-0.1.11-1PGSTY.el8.x86_64.rpm
jsonschema_160.1.11el8.aarch64pigsty1.5 MiBjsonschema_16-0.1.11-1PGSTY.el8.aarch64.rpm
jsonschema_160.1.11el9.x86_64pigsty1.9 MiBjsonschema_16-0.1.11-2PGSTY.el9.x86_64.rpm
jsonschema_160.1.11el9.aarch64pigsty1.8 MiBjsonschema_16-0.1.11-2PGSTY.el9.aarch64.rpm
jsonschema_160.1.11el10.x86_64pigsty1.9 MiBjsonschema_16-0.1.11-2PGSTY.el10.x86_64.rpm
jsonschema_160.1.11el10.aarch64pigsty1.8 MiBjsonschema_16-0.1.11-2PGSTY.el10.aarch64.rpm
postgresql-16-jsonschema0.1.11d12.x86_64pigsty1.3 MiBpostgresql-16-jsonschema_0.1.11-2PGSTY~bookworm_amd64.deb
postgresql-16-jsonschema0.1.11d12.aarch64pigsty1.1 MiBpostgresql-16-jsonschema_0.1.11-2PGSTY~bookworm_arm64.deb
postgresql-16-jsonschema0.1.11d13.x86_64pigsty1.3 MiBpostgresql-16-jsonschema_0.1.11-2PGSTY~trixie_amd64.deb
postgresql-16-jsonschema0.1.11d13.aarch64pigsty1.1 MiBpostgresql-16-jsonschema_0.1.11-2PGSTY~trixie_arm64.deb
postgresql-16-jsonschema0.1.11u22.x86_64pigsty1.4 MiBpostgresql-16-jsonschema_0.1.11-2PGSTY~jammy_amd64.deb
postgresql-16-jsonschema0.1.11u22.aarch64pigsty1.3 MiBpostgresql-16-jsonschema_0.1.11-2PGSTY~jammy_arm64.deb
postgresql-16-jsonschema0.1.11u24.x86_64pigsty1.4 MiBpostgresql-16-jsonschema_0.1.11-2PGSTY~noble_amd64.deb
postgresql-16-jsonschema0.1.11u24.aarch64pigsty1.3 MiBpostgresql-16-jsonschema_0.1.11-2PGSTY~noble_arm64.deb
postgresql-16-jsonschema0.1.11u26.x86_64pigsty1.4 MiBpostgresql-16-jsonschema_0.1.11-2PGSTY~resolute_amd64.deb
postgresql-16-jsonschema0.1.11u26.aarch64pigsty1.3 MiBpostgresql-16-jsonschema_0.1.11-2PGSTY~resolute_arm64.deb
软件包版本平台仓库大小下载
jsonschema_150.1.11el8.x86_64pigsty1.6 MiBjsonschema_15-0.1.11-1PGSTY.el8.x86_64.rpm
jsonschema_150.1.11el8.aarch64pigsty1.5 MiBjsonschema_15-0.1.11-1PGSTY.el8.aarch64.rpm
jsonschema_150.1.11el9.x86_64pigsty1.9 MiBjsonschema_15-0.1.11-2PGSTY.el9.x86_64.rpm
jsonschema_150.1.11el9.aarch64pigsty1.8 MiBjsonschema_15-0.1.11-2PGSTY.el9.aarch64.rpm
jsonschema_150.1.11el10.x86_64pigsty1.9 MiBjsonschema_15-0.1.11-2PGSTY.el10.x86_64.rpm
jsonschema_150.1.11el10.aarch64pigsty1.8 MiBjsonschema_15-0.1.11-2PGSTY.el10.aarch64.rpm
postgresql-15-jsonschema0.1.11d12.x86_64pigsty1.3 MiBpostgresql-15-jsonschema_0.1.11-2PGSTY~bookworm_amd64.deb
postgresql-15-jsonschema0.1.11d12.aarch64pigsty1.1 MiBpostgresql-15-jsonschema_0.1.11-2PGSTY~bookworm_arm64.deb
postgresql-15-jsonschema0.1.11d13.x86_64pigsty1.3 MiBpostgresql-15-jsonschema_0.1.11-2PGSTY~trixie_amd64.deb
postgresql-15-jsonschema0.1.11d13.aarch64pigsty1.1 MiBpostgresql-15-jsonschema_0.1.11-2PGSTY~trixie_arm64.deb
postgresql-15-jsonschema0.1.11u22.x86_64pigsty1.4 MiBpostgresql-15-jsonschema_0.1.11-2PGSTY~jammy_amd64.deb
postgresql-15-jsonschema0.1.11u22.aarch64pigsty1.3 MiBpostgresql-15-jsonschema_0.1.11-2PGSTY~jammy_arm64.deb
postgresql-15-jsonschema0.1.11u24.x86_64pigsty1.4 MiBpostgresql-15-jsonschema_0.1.11-2PGSTY~noble_amd64.deb
postgresql-15-jsonschema0.1.11u24.aarch64pigsty1.3 MiBpostgresql-15-jsonschema_0.1.11-2PGSTY~noble_arm64.deb
postgresql-15-jsonschema0.1.11u26.x86_64pigsty1.4 MiBpostgresql-15-jsonschema_0.1.11-2PGSTY~resolute_amd64.deb
postgresql-15-jsonschema0.1.11u26.aarch64pigsty1.3 MiBpostgresql-15-jsonschema_0.1.11-2PGSTY~resolute_arm64.deb

源码

pig build pkg jsonschema;		# build rpm/deb

安装

启用 PGDG 和 PIGSTY 仓库并更新缓存。

pig repo add pgsql -u   # add both repo and update cache

使用 pig 安装扩展。

pig install jsonschema;		# install by package name

pig install jsonschema -v 19;   # install for PG 19
pig install jsonschema -v 18;   # install for PG 18
pig install jsonschema -v 17;   # install for PG 17
pig install jsonschema -v 16;   # install for PG 16
pig install jsonschema -v 15;   # install for PG 15

在数据库中创建扩展。

CREATE EXTENSION jsonschema;

用法

来源:

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 之外没有其他依赖。

0.1.11 支持 PostgreSQL 13–19,使用 pgrx 0.19.3。安装要求超级用户,无需共享预加载。扩展禁用远程模式获取;引用的模式须显式提供。

校验 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 省略 $schema,jsonschema.default_draft 会控制默认 draft。支持值为 V4、V6、V7、V2019 和 V2020。

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

表格阅读器