json模式验证具有anyOf和oneOf需求的对象数组
我试图定义一个json模式来限制数组中包含的对象的属性 到目前为止,我得到的是:json模式验证具有anyOf和oneOf需求的对象数组,json,validation,jsonschema,Json,Validation,Jsonschema,我试图定义一个json模式来限制数组中包含的对象的属性 到目前为止,我得到的是: { "title":"myCollection", "properties":{ "things":{ "type":"array", "items":[{ "title":"thingObj", "type":"object", "prop
{
"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
}
]
}
这提供了一个有效的响应,但此验证似乎只适用于数组中的第一个元素。如果删除第一个元素上的anyOf子句或oneOf子句中包含的所有字段,则验证将失败。第二个数组元素上的相同操作不会引发所需的故障。如何确保对每个数组成员运行验证?这是因为您(意外)使用了“元组类型”。当“items”
的值是一个数组,并且它将架构与数组中的特定位置相匹配时,将启用此选项
如果您将
“项”
(在您的模式中)更改为一个简单的模式(而不是模式数组),那么它将以相同的方式验证所有项。七年前您就回答了这个问题,但是您能更详细地说明“如果您更改”项(在您的模式中)是指一个简单的模式(而不是模式数组)吗,然后它将以相同的方式验证所有项。“。我一直在尝试定义一个数组,其中可以有不同数量的项遵循相同的模式选择。