我需要构建一个json模式(草案4),它需要一个基于另一个嵌套对象中属性存在的属性 . 我已经搜索并尝试了很多东西(anyOf,oneOf,not,依赖)没有运气 .
也许这在json架构中是不可能的?
这是我简化的架构:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["dog"],
"properties": {
"dog": {
"type": "object",
"required": ["bananas"],
"properties": {
"bananas": { "$ref": "bananas.json" },
"thing": {
"type": "object",
"properties": {
"total": { "type": "string" }
}
}
}
}
}
}
这是bananas.json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["banana"],
"definitions": {
"non-empty-string": {
"type": "string",
"minLength": 1
}
},
"properties": {
"banana": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"required": ["unit"],
"properties": {
"unit": { "type": "string" },
"thing": {
"type": "object",
"anyOf": [
{ "required": [ "tax_transfers" ] },
{ "required": [ "tax_retentions" ] }
],
"properties": {
"tax_transfers": {
"type": "object",
"required": ["tax_transfer"],
"properties": {
"tax_transfer": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"rate": { "type": "string" }
}
}
}
}
},
"tax_retentions": {
"type": "object",
"required": ["tax_retention"],
"properties": {
"tax_retention": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"rate": { "type": "string" }
}
}
}
}
}
}
}
}
}
}
}
}
当数组中的一个或多个对象具有'thing'属性(在香蕉 - >香蕉 - >事物中)时,我需要它 . 然后应该要求(狗 - >事物)属性'东西' .
任何帮助将非常感激 .
1 回答
你需要两件事来表达你的约束 . 第一个是"contains",另一个是"implication" . 我在
definitions
部分组织了每一个 .Contains
items
关键字允许我们要求数组中的所有项对模式有效 . 如果数组中的所有项都不对模式有效,那么我们知道至少有一个项是有效的 .如果您能够升级到JSON Schema draft-06,则会添加一个
contains
关键字,以便更轻松 .Implication
暗示允许您执行类似条件的操作 . 如果条件为真,或者约束为真(或两者都为真),则条件模式意味着约束模式 . 它实际上与说法相同,如果条件为真,那么约束也必须为真 .
JSON Schema draft-07添加了
if
-then
-else
关键字,试图更好地解决这种情况 . 我个人不喜欢这样做的方式,我会坚持这种事情的蕴涵模式,但在这里,以防万一你想尝试它 .All together