JSON 基于 属性 的架构验证
JSON Schema Validation based on a property
我一直在努力使我的 JSON 架构正确。我有一个 boolean
属性,我必须根据它来确定所需的属性。下面是我的示例 JSON
,我想通过 item3
不存在的验证失败。
{
"item1": true,
"item2": "ABC"
}
这是我希望验证通过的 JSON
{
"item1": true,
"item2": "ABC",
"item3": {
"subItem1": "ABC",
"subItem2": "BAC"
}
}
同样,如果 item1
是 false
,那么上述两个 JSON 的验证都应该通过。
我的 JSON 架构如下。
{
"definitions": {},
"type": "object",
"title": "The Root Schema",
"properties": {
"item1": {
"$id": "#/properties/item1",
"type": "boolean",
"title": "The Item1 Schema",
"default": false,
"examples": [
true
]
},
"item2": {
"$id": "#/properties/item2",
"type": "string",
"title": "The Item2 Schema",
"default": "",
"examples": [
"ABC"
],
"pattern": "^(.*)$"
},
"item3": {
"$id": "#/properties/item3",
"type": "object",
"title": "The Item3 Schema",
"required": [
"subItem1",
"subItem2"
],
"properties": {
"subItem1": {
"$id": "#/properties/item3/properties/subItem1",
"type": "string",
"title": "The Subitem1 Schema",
"default": "",
"examples": [
"AAA"
],
"pattern": "^(.*)$"
},
"subItem2": {
"$id": "#/properties/item3/properties/subItem2",
"type": "string",
"title": "The Subitem2 Schema",
"default": "",
"examples": [
"BAC"
],
"pattern": "^(.*)$"
}
}
}
},
"required": ["item1"],
"allOf": [{
"if": {
"properties": {
"item1": {
"enum": [
true
]
}
}
},
"then": {
"required": [
"item2",
"item3"
]
},
"else": {
"required": [
"item2"
]
}
}]
}
我的验证总是失败。
如果 item1
为真,则应要求 subItem2
。
如果 item1
为 false,则不需要 item3
,但如果包含,仍应验证。
您的 if
/then
/else
块在验证方面工作正常。
您提供的您希望通过的示例 JSON 失败了,因为您要求 item3
具有 subItem1
和 [=16= 的 属性 ],但事实并非如此。
现在您已经更新了您的示例 JSON,它应该通过正确的 item3
包含 subItem1
和 subItem2
,验证通过您提供的架构.
另外,你要,如果我没理解错的话:
If item1 is true, subItem2 should be required. If item1 is false, then
item3 is not required, but should still validate if included.
将 subItem3
所需的架构从 item3
移动到您的 then
子句。如果您的 if
模式验证成功(item1
是 true
)
,这将使 subItem3
仅是 "required"
{
"definitions": {},
"type": "object",
"title": "The Root Schema",
"properties": {
"item1": {
"$id": "#/properties/item1",
"type": "boolean",
"title": "The Item1 Schema",
"default": false,
"examples": [
true
]
},
"item2": {
"$id": "#/properties/item2",
"type": "string",
"title": "The Item2 Schema",
"default": "",
"examples": [
"ABC"
],
"pattern": "^(.*)$"
},
"item3": {
"$id": "#/properties/item3",
"type": "object",
"title": "The Item3 Schema",
"required": [
"subItem1"
],
"properties": {
"subItem1": {
"$id": "#/properties/item3/properties/subItem1",
"type": "string",
"title": "The Subitem1 Schema",
"default": "",
"examples": [
"AAA"
],
"pattern": "^(.*)$"
},
"subItem2": {
"$id": "#/properties/item3/properties/subItem2",
"type": "string",
"title": "The Subitem2 Schema",
"default": "",
"examples": [
"BAC"
],
"pattern": "^(.*)$"
}
}
}
},
"required": [
"item1"
],
"allOf": [
{
"if": {
"properties": {
"item1": {
"enum": [
true
]
}
}
},
"then": {
"required": [
"item2",
"item3"
],
"properties": {
"item3": {
"required": [
"subItem2"
]
}
}
},
"else": {
"required": [
"item2"
]
}
}
]
}
我一直在努力使我的 JSON 架构正确。我有一个 boolean
属性,我必须根据它来确定所需的属性。下面是我的示例 JSON
,我想通过 item3
不存在的验证失败。
{
"item1": true,
"item2": "ABC"
}
这是我希望验证通过的 JSON
{
"item1": true,
"item2": "ABC",
"item3": {
"subItem1": "ABC",
"subItem2": "BAC"
}
}
同样,如果 item1
是 false
,那么上述两个 JSON 的验证都应该通过。
我的 JSON 架构如下。
{
"definitions": {},
"type": "object",
"title": "The Root Schema",
"properties": {
"item1": {
"$id": "#/properties/item1",
"type": "boolean",
"title": "The Item1 Schema",
"default": false,
"examples": [
true
]
},
"item2": {
"$id": "#/properties/item2",
"type": "string",
"title": "The Item2 Schema",
"default": "",
"examples": [
"ABC"
],
"pattern": "^(.*)$"
},
"item3": {
"$id": "#/properties/item3",
"type": "object",
"title": "The Item3 Schema",
"required": [
"subItem1",
"subItem2"
],
"properties": {
"subItem1": {
"$id": "#/properties/item3/properties/subItem1",
"type": "string",
"title": "The Subitem1 Schema",
"default": "",
"examples": [
"AAA"
],
"pattern": "^(.*)$"
},
"subItem2": {
"$id": "#/properties/item3/properties/subItem2",
"type": "string",
"title": "The Subitem2 Schema",
"default": "",
"examples": [
"BAC"
],
"pattern": "^(.*)$"
}
}
}
},
"required": ["item1"],
"allOf": [{
"if": {
"properties": {
"item1": {
"enum": [
true
]
}
}
},
"then": {
"required": [
"item2",
"item3"
]
},
"else": {
"required": [
"item2"
]
}
}]
}
我的验证总是失败。
如果 item1
为真,则应要求 subItem2
。
如果 item1
为 false,则不需要 item3
,但如果包含,仍应验证。
您的 if
/then
/else
块在验证方面工作正常。
您提供的您希望通过的示例 JSON 失败了,因为您要求 item3
具有 subItem1
和 [=16= 的 属性 ],但事实并非如此。
现在您已经更新了您的示例 JSON,它应该通过正确的 item3
包含 subItem1
和 subItem2
,验证通过您提供的架构.
另外,你要,如果我没理解错的话:
If item1 is true, subItem2 should be required. If item1 is false, then item3 is not required, but should still validate if included.
将 subItem3
所需的架构从 item3
移动到您的 then
子句。如果您的 if
模式验证成功(item1
是 true
)
subItem3
仅是 "required"
{
"definitions": {},
"type": "object",
"title": "The Root Schema",
"properties": {
"item1": {
"$id": "#/properties/item1",
"type": "boolean",
"title": "The Item1 Schema",
"default": false,
"examples": [
true
]
},
"item2": {
"$id": "#/properties/item2",
"type": "string",
"title": "The Item2 Schema",
"default": "",
"examples": [
"ABC"
],
"pattern": "^(.*)$"
},
"item3": {
"$id": "#/properties/item3",
"type": "object",
"title": "The Item3 Schema",
"required": [
"subItem1"
],
"properties": {
"subItem1": {
"$id": "#/properties/item3/properties/subItem1",
"type": "string",
"title": "The Subitem1 Schema",
"default": "",
"examples": [
"AAA"
],
"pattern": "^(.*)$"
},
"subItem2": {
"$id": "#/properties/item3/properties/subItem2",
"type": "string",
"title": "The Subitem2 Schema",
"default": "",
"examples": [
"BAC"
],
"pattern": "^(.*)$"
}
}
}
},
"required": [
"item1"
],
"allOf": [
{
"if": {
"properties": {
"item1": {
"enum": [
true
]
}
}
},
"then": {
"required": [
"item2",
"item3"
],
"properties": {
"item3": {
"required": [
"subItem2"
]
}
}
},
"else": {
"required": [
"item2"
]
}
}
]
}