[英]json schema to validate array of objects with anyOf and oneOf requirements
我正在嘗試定義一個 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
}]
}
}
}
這是因為你(不小心)使用了“元組輸入”。 當"items"
的值是一個數組時,它會啟用,並且它將模式與數組中的特定位置匹配。
如果將( "items"
(在模式中)更改為模式(不是模式數組),那么它將以相同的方式驗證所有項目。
感謝@cloudfeet 的回答,在看到他的回答之前,我一直在努力解決這個問題。 為了更清楚,應該刪除項目周圍的 []。
{
"title":"myCollection",
"properties":{
"things":{
"type":"array",
"items":**[**{
"title":"thingObj",
"type":"object",
.
.
.
"required":["value"]
}**]**,
"additionalProperties":false
}]
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.