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

如何为不同父对象的引用Schema设置差异化必填字段?

实现JSON Schema中不同父对象下的组件字段必填差异

要达成你需要的效果,只需给software字段的定义使用allOf关键字,先复用基础的component schema,再额外添加version的必填要求即可,修改后的完整Schema如下:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "properties": {
        "metadata": {
          "$ref": "#/definitions/metadata",
          "title": "Metadata",
          "description": "Provides additional information about a ABC."
        }
    },
              
    "definitions": {
        "metadata": {
          "type": "object",
          "title": "BOM Metadata Object",
          "properties": {
              "software": {
                "title": "Software",
                "description": "The component that the ABC describes.",
                "allOf": [
                  {"$ref": "#/definitions/component"},
                  {"required": ["version"]}
                ]
              },
                
              "tool" : {
                "items": {"$ref": "#/definitions/component"}
              }
          }
        },
        
        "component": {
            "type": "object",
            "title": "Component Object",
            "required" : ["name"],
            "properties": {
                "name": {
                  "type": "string",
                  "title": "Component Name"
                },
                "version": {
                  "type": "string",
                  "title": "Component Version"
                }
            }
        }
    }
}

关键修改说明

  • software字段:通过allOf组合两条规则,第一条引用基础component(自带name必填要求),第二条新增version为必填项,叠加后software就同时要求name和version都必填。
  • tool字段:保持原有引用方式,因此其中的每个component仍然仅要求name必填,完全匹配你的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 04:20:06