如何设置JSON Schema规则以确定必须设置其中一个属性并且是必需的?
我尝试了各种方法来解决它:
{
"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"
}
}
}
Run Code Online (Sandbox Code Playgroud)
但它从来没有奏效.我需要做的是接受station_id什么是整数或者什么是字符串名称.
请问有办法吗?
clo*_*eet 22
oneOf直接在模式中使用时才特殊.当你oneOf在内部使用时properties,它没有特殊含义,所以你实际上最终定义了一个名为的属性"oneOf".
此外 - 它不是属性定义,需要一些东西,它是required关键字.此关键字是必需属性的数组(不是布尔值,即旧语法).
为了做你想做的事,你做一个需要oneOf一个选项的子句,另一个需要:"station_id""station"
{
"oneOf": [
{"required": ["station"]},
{"required": ["station_id"]}
]
}
Run Code Online (Sandbox Code Playgroud)
如果两者都存在,则数据将无效(因为只oneOf允许一个条目通过).
| 归档时间: |
|
| 查看次数: |
5320 次 |
| 最近记录: |