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

如何使用JSON Schema的dependentRequired绑定不同对象中的属性?

如何用JSON Schema实现跨对象属性的依赖要求

dependentRequired本身只能作用在当前对象内部的属性依赖,没法直接关联不同层级对象里的属性,所以得用if/then条件判断来实现你要的逻辑。

直接上可用的Schema示例:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "profile": {
      "type": "object",
      "properties": {
        "email": { "type": "string", "format": "email" },
        "password": { "type": "string" }
      }
    },
    "bearer": { "type": "string" }
  },
  "if": {
    "anyOf": [
      { "required": ["profile.email"] },
      { "required": ["profile.password"] }
    ]
  },
  "then": {
    "required": ["bearer"]
  }
}

逻辑说明:

  • if块通过anyOf判断两种触发情况:只要profile.email或profile.password存在(被标记为必填),就进入then分支。
  • then块强制要求必须包含bearer属性,从而实现跨对象的依赖绑定。

你之前用绝对路径$ref失败的原因是:dependentRequired的属性值只能是当前对象的直接属性名,不支持路径格式的引用,所以这种跨层级的场景必须用条件判断来实现。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 21:53:07