如何设置 JSON 架构规则来表明必须设置并且是必需的属性之一?
我尝试了各种方法来解决它,例如:
{
"id":"#",
"required":true,
"additionalProperties":true,
"type":"object",
"properties":{
"surname":{
"id":"surname",
"required":true,
"type":"string"
},
"oneOf":[
{
"$ref":"#/definitions/station_id"
},
{
"$ref":"#/definitions/station"
}
]
},
"definitions":{
"station_id":{
"type":"integer"
},
"station":{
"type":"string"
}
}
}
但它从来没有奏效。我需要做的是接受 station_id (整数)或 station (字符串名称)。
请问有办法做到吗?
oneOf
仅当直接在模式内使用时才特殊。当你使用oneOf
inside properties
,那么它没有特殊含义,所以你实际上最终定义了一个名为"oneOf"
反而。
另外 - 要求某些东西的不是属性定义,而是required
关键词。该关键字是必需属性的数组(不是布尔值,这是旧语法)。
为了做你想做的事,你做了一个oneOf
其中一个选项有的子句"station_id"
需要,另一个有"station"
必需的:
{
"oneOf": [
{"required": ["station"]},
{"required": ["station_id"]}
]
}
If both存在,那么数据将无效(因为只有一个oneOf
允许通过)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)