You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

是否存在可验证JSON数组Schema定义的公开JSON Schema?

验证数组类型JSON Schema的方案

当然可以实现这个需求——你可以基于JSON Schema的元规范,定制一个专门用来验证「数组类型Schema」的Schema。核心思路是先确保目标Schema符合JSON Schema的基本格式,再强制它满足数组类型的特定要求(比如指定type为array、包含name和items字段)。

下面是一个直接可用的Schema示例,针对JSON Schema Draft 07版本:

{
  "type": "object",
  "required": ["type", "name", "items"],
  "properties": {
    "type": {
      "type": "string",
      "enum": ["array"]
    },
    "name": {
      "type": "string"
    },
    "items": {
      "oneOf": [
        { "$ref": "#" },
        { "type": "array", "items": { "$ref": "#" } }
      ]
    }
  },
  "additionalProperties": true
}

关键规则说明:

  • required数组强制要求目标Schema必须包含type、name、items三个字段,满足你说的「超集」基础要求
  • type字段被限定为字符串"array",确保这是一个数组类型的Schema定义
  • items字段支持两种合法格式:单个JSON Schema对象,或者多个Schema对象组成的数组(符合JSON Schema原生对items的定义)
  • additionalProperties: true允许目标Schema包含其他额外字段(比如description、minItems等),保证它是数组Schema的超集

示例验证:

  • 符合要求的Schema:
{
  "name": "tags",
  "type": "array",
  "items": { "type": "string" },
  "minItems": 1
}
  • 不符合要求的情况:
    • 缺少items字段 → 验证失败
    • type设为"string" → 验证失败
    • name是数字而非字符串 → 验证失败

这个方案完全基于JSON Schema原生规范,不需要依赖外部工具或第三方Schema,直接就能用来验证用户提交的数组类型Schema定义。

内容的提问来源于stack exchange,提问作者John Teague

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 18:22:37