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

如何用JSON Schema匹配空数组或含指定双字段的对象数组?

解决JSON Schema数组元素字段必选问题

你的需求是让数组要么为空,要么每个元素都同时包含text和number字段,只需要在items对应的对象Schema里添加required关键字,指定必须包含的字段列表即可。

修改后的完整Schema如下:

"something": {
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "text": {"type": "string"},
      "number": {"type": "integer"}
    },
    "required": ["text", "number"],
    "additionalProperties": false
  }
}

关键说明

  • required: ["text", "number"] 强制每个数组对象必须同时拥有这两个字段,缺少任意一个都会校验失败
  • additionalProperties: false 确保对象不能包含text和number之外的其他字段
  • 空数组[]依然会被正常接受,因为array类型默认允许空数组,无需额外配置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 10:04:53