JSON Schema依赖性:枚举内容取决于从另一个枚举中选择 [英] JSON Schema dependencies: enum content depends on choice from another enum

查看:162
本文介绍了JSON Schema依赖性:枚举内容取决于从另一个枚举中选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个enum,带有第一组选项,第二个enum的内容取决于在第一个enum中所做的选择.

I have an enum with a first set of choices, and a second enum whose content depends on the choice made in the first enum.

这是我目前拥有的一个简单示例(不正确):

Here's a simple example of what I have at present (not right):

"fontGroup": {
    "title": "Font Group",
    "type": "string",
    "enum": [
        "Roboto",
        "Noto",
        "Alegreya"
    ],
    "default": "Roboto"
},
"fontFamily": {
    "title": "Font Family",
    "type": "string",
    "enum": [
        "Roboto Slab",
        "Roboto Condensed",
        "---",
        "Noto Sans",
        "Noto Serif",
        "---",
        "Alegreya SC",
        "Alegreya Sans"
    ],
    "default": "Roboto Slab"
}

当然,如果从第一个enum中选择了Noto,则只有第二个enum中与Noto相关的选择才有效.例如,与Roboto Condensed一起选择Noto是无效的.

Of course, if Noto is selected from the first enum then only the Noto related choices in the second enum are valid. It is invalid to select Noto in combination with Roboto Condensed, for example.

如何在模式中指定它?

推荐答案

您不能引用draft-07及以下版本的相对属性,但可以枚举所有可能的对象变体:

You can not refer to a relative property as of draft-07 and below, but you can enumerate all possible object variations:

{
  "type": "object",
  "oneOf": [
    {
      "properties": {
        "fontGroup": {
          "const": "Roboto"
        },
        "fontFamily": {
          "enum": [
            "Roboto Slab",
            "Roboto Condenced"
          ]
        }
      }
    },
    {
      "properties": {
        "fontGroup": {
          "const": "Noto"
        },
        "fontFamily": {
          "enum": [
            "Noto Sans",
            "Noto Serif"
          ]
        }
      }
    },
    {
      "properties": {
        "fontGroup": {
          "const": "Alegreya"
        },
        "fontFamily": {
          "enum": [
            "Alegreya SC",
            "Alegreya Sans"
          ]
        }
      }
    }
  ]
}

const关键字在draft-04中不可用,您可以将其更改为单值枚举:"enum":["Roboto"].

const keyword is not available in draft-04, you can change it to single-value enum: "enum":["Roboto"].

这篇关于JSON Schema依赖性:枚举内容取决于从另一个枚举中选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆