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

JSON Schema依赖枚举问询:二级枚举内容随一级枚举变化

正确实现依赖型枚举的JSON Schema方案

这个问题我太熟了!你现在把所有二级字体选项硬塞在一个枚举里用---分隔的做法,确实不是JSON Schema的正确玩法。要实现这种依赖型的枚举关联,咱们可以用JSON Schema标准里的if/then/else或者dependentSchemas关键字来搞定,比你现在的方法规范多了,还能自动做校验。

方法一:使用if/then条件校验(推荐,逻辑直观清晰)

这种方式通过判断fontGroup的具体取值,动态约束fontFamily的可选范围,非常适合多层级的依赖场景:

{
  "type": "object",
  "properties": {
    "fontGroup": {
      "title": "Font Group",
      "type": "string",
      "enum": ["Roboto", "Noto", "Alegreya"],
      "default": "Roboto"
    },
    "fontFamily": {
      "title": "Font Family",
      "type": "string"
    }
  },
  "if": {
    "properties": {
      "fontGroup": { "const": "Roboto" }
    }
  },
  "then": {
    "properties": {
      "fontFamily": {
        "enum": ["Roboto Slab", "Roboto Condensed"],
        "default": "Roboto Slab"
      }
    }
  },
  "else": {
    "if": {
      "properties": {
        "fontGroup": { "const": "Noto" }
      }
    },
    "then": {
      "properties": {
        "fontFamily": {
          "enum": ["Noto Sans", "Noto Serif"],
          "default": "Noto Sans"
        }
      }
    },
    "else": {
      "if": {
        "properties": {
          "fontGroup": { "const": "Alegreya" }
        }
      },
      "then": {
        "properties": {
          "fontFamily": {
            "enum": ["Alegreya SC", "Alegreya Sans"],
            "default": "Alegreya SC"
          }
        }
      }
    }
  },
  "required": ["fontGroup", "fontFamily"]
}

简单解释下:

  • 先定义基础的fontGroup枚举,fontFamily暂时只声明类型,不固定枚举值
  • 通过多层if/then/else判断fontGroup的取值,为每个分组单独指定fontFamily的可选选项和默认值
  • 最后用required关键字确保两个字段都必须存在(如果允许可选的话可以去掉这个配置)

方法二:使用dependentSchemas(适合简单依赖场景)

如果你的依赖关系比较直接,也可以用dependentSchemas来绑定字段的约束规则,写法更紧凑:

{
  "type": "object",
  "properties": {
    "fontGroup": {
      "title": "Font Group",
      "type": "string",
      "enum": ["Roboto", "Noto", "Alegreya"],
      "default": "Roboto"
    },
    "fontFamily": {
      "title": "Font Family",
      "type": "string"
    }
  },
  "dependentSchemas": {
    "fontGroup": {
      "oneOf": [
        {
          "properties": {
            "fontGroup": { "const": "Roboto" },
            "fontFamily": {
              "enum": ["Roboto Slab", "Roboto Condensed"],
              "default": "Roboto Slab"
            }
          }
        },
        {
          "properties": {
            "fontGroup": { "const": "Noto" },
            "fontFamily": {
              "enum": ["Noto Sans", "Noto Serif"],
              "default": "Noto Sans"
            }
          }
        },
        {
          "properties": {
            "fontGroup": { "const": "Alegreya" },
            "fontFamily": {
              "enum": ["Alegreya SC", "Alegreya Sans"],
              "default": "Alegreya SC"
            }
          }
        }
      ]
    }
  },
  "required": ["fontGroup", "fontFamily"]
}

简单解释下:

  • dependentSchemas表示当fontGroup存在时,必须满足oneOf里的某一组约束
  • 每个子项同时指定fontGroup的固定值和对应的fontFamily枚举,确保两者的匹配关系

为什么你的原方法不推荐?

  • 分隔符---会被当作合法选项,用户可能会误选
  • JSON Schema无法自动校验fontFamily和fontGroup的对应关系,完全靠人工判断
  • 原代码里的default: "Roboto..."是无效的,没法自动填充对应分组的默认值

这两种方法都符合JSON Schema的标准规范,主流的校验工具(比如Ajv)都能完美支持,选哪种看你自己的习惯和场景复杂度就行~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:08:02