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

如何在JSON Schema中根据kind字段值条件修改uuid字段类型

条件规则添加位置

你需要把条件校验逻辑加在整个JSON Schema的最外层根对象层级,和现有根级的type、properties字段平级,不要嵌套到bucket、uuid等子属性的定义内部——因为你要判断的kind字段是根层级的属性,条件规则放在根层级才能正确读取kind的值做判断。

具体实现代码

下面是补全条件规则后的完整Schema,完全保留你原有结构,只新增了必要的条件逻辑:

{
  "type": "object",
  "properties": {
    "kind": { "type": "string" },
    "bucket": {
      "items": {
        "anyOf": [
          {
            "properties": {
              "properties": {
                "properties": {
                  "uuid": { "type": "string" }
                }
              }
            }
          }
        ]
      }
    }
  },
  /* 以下为新增的条件校验逻辑,和上方type、properties平级 */
  "if": {
    "properties": {
      "kind": { "const": "OPTIONAL" }
    },
    "required": ["kind"]
  },
  "then": {
    "properties": {
      "bucket": {
        "items": {
          "anyOf": [
            {
              "properties": {
                "properties": {
                  "properties": {
                    "uuid": { "type": ["null", "string"] }
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}
关键说明
  • 原有uuid默认"type": "string"的约束不需要修改:当kind值不等于OPTIONAL时,then块的规则不会生效,自动沿用默认的类型约束。
  • if块里必须加"required": ["kind"]:避免kind字段缺失时误触发OPTIONAL对应的规则。
  • 不要随意调整条件块的嵌套层级:JSON Schema的if/then规则生效范围和它所在的层级绑定,放在子属性里就无法读取同级以外的字段值做判断。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.01 12:01:04