基于 JSON 架构中枚举值的属性
Properties based on enum value in JSON Schema
我正在构建一个 json 模式定义,它有一组固定的控件,我目前使用 enum
限制了这些控件。但是,并非所有属性都与所有控件相关。
如果 controlType
= dropdown
,我只想要求 options
属性
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"controlType": {
"type": "string",
"enum": ["title", "dropdown", "button"]
},
"options:": {
"type": "array",
"items": {"type": "string"}
}
}
}
}
如何在 json 架构中有条件地包含/要求字段?
使用IF..Then..Else
new in Draft-07
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items":{
"type": "object",
"properties":{
"controlType":{
"type": "string",
"enum": ["title", "dropdown", "button"]
},
"options:":{
"type": "array",
"items":{"type":"string"}
}
},
<b>
"if":{
"properties":{
"controlType":{"const":"dropdown"}
}
},
"then":{
"required": ["options"]
}</b>
}
}
使用oneOf
or anyOf
如果您的 属性 具有有限数量的可接受值(例如枚举),但每个可能的值都需要单独映射,这会很有用。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items":{
"type": "object",
"properties":{
"controlType":{
"type": "string",
"enum": ["title", "dropdown", "button"]
},
"options:":{
"type": "array",
"items":{"type":"string"}
}
},<b>
"anyOf": [
{
"properties":{
"controlType":{"const":"dropdown"}
},
"required": ["controlType", "options"]
},
{
"properties":{
"controlType":{"const":"title"}
},
"required": ["controlType"]
},
{
"properties":{
"controlType":{"const":"button"}
},
"required": ["controlType"]
}
]</b>
}
}
进一步阅读
我正在构建一个 json 模式定义,它有一组固定的控件,我目前使用 enum
限制了这些控件。但是,并非所有属性都与所有控件相关。
如果 controlType
= dropdown
options
属性
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"controlType": {
"type": "string",
"enum": ["title", "dropdown", "button"]
},
"options:": {
"type": "array",
"items": {"type": "string"}
}
}
}
}
如何在 json 架构中有条件地包含/要求字段?
使用IF..Then..Else
new in Draft-07
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items":{
"type": "object",
"properties":{
"controlType":{
"type": "string",
"enum": ["title", "dropdown", "button"]
},
"options:":{
"type": "array",
"items":{"type":"string"}
}
},
<b>
"if":{
"properties":{
"controlType":{"const":"dropdown"}
}
},
"then":{
"required": ["options"]
}</b>
}
}
使用oneOf
or anyOf
如果您的 属性 具有有限数量的可接受值(例如枚举),但每个可能的值都需要单独映射,这会很有用。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items":{
"type": "object",
"properties":{
"controlType":{
"type": "string",
"enum": ["title", "dropdown", "button"]
},
"options:":{
"type": "array",
"items":{"type":"string"}
}
},<b>
"anyOf": [
{
"properties":{
"controlType":{"const":"dropdown"}
},
"required": ["controlType", "options"]
},
{
"properties":{
"controlType":{"const":"title"}
},
"required": ["controlType"]
},
{
"properties":{
"controlType":{"const":"button"}
},
"required": ["controlType"]
}
]</b>
}
}