JSON Schema依赖性:枚举内容取决于从另一个枚举中选择 [英] JSON Schema dependencies: enum content depends on choice from another enum
问题描述
我有一个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屋!