JSON Schema dependencies: enum content depends on choice from another enum

我的未来我决定 提交于 2019-12-11 07:27:23

问题


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"
}

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.

How can this be specified in the schema?


回答1:


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 keyword is not available in draft-04, you can change it to single-value enum: "enum":["Roboto"].



来源:https://stackoverflow.com/questions/50228218/json-schema-dependencies-enum-content-depends-on-choice-from-another-enum

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!