【发布时间】:2013-11-20 15:50:54
【问题描述】:
我正在尝试定义一个 json 模式来限制数组中包含的对象的属性。
到目前为止我所拥有的是:
{
"title":"myCollection",
"properties":{
"things":{
"type":"array",
"items":[{
"title":"thingObj",
"type":"object",
"properties":{
"name":{
"type":"string"
},
"code":{
"type":"string"
},
"type":{
"type":"string",
"enum":["dog","cat"]
},
"rate":{
"type":"number"
},
"value":{
"type":"number"
}
},
"anyOf":[{
"properties":{
"name":{
"type":"string"
}
},"required":["name"]
},{
"properties":{
"code":{
"type":"string"
}
},"required":["code"]
},{
"properties":{
"type":{
"type":"string",
"enum":["new","existing"]
}
},"required":["type"]
}],
"oneOf":[{
"properties":{
"rate":{
"type":"number"
}
},
"required":["rate"]
},{
"properties":{
"value":{
"type":"number"
}
},
"required":["value"]
}],
"additionalProperties":false
}]
}
}
}
现在给出以下 jsonobj:
{
"things": [
{
"name": "tabby",
"code": "meeow",
"type": "cat",
"value": 20
},
{
"name": "k9",
"code": "woofer",
"type": "dog",
"rate": 15
}
]
}
这个json schema validator 提供了一个有效的响应,但这个验证似乎只适用于数组中的第一个元素。如果删除第一个元素的 anyOf 子句或 oneOf 子句中包含的所有字段,则验证失败。第二个数组元素上的相同不会引发所需的失败。如何确保针对每个数组成员运行验证?
【问题讨论】:
标签: json validation jsonschema