基于属性的JSON模式验证 [英] JSON Schema Validation based on a property
问题描述
我一直在尝试正确设置JSON模式.我有一个boolean
属性,必须根据该属性确定所需的属性.以下是我的示例JSON
,我想在没有item3
的情况下使验证失败.
I have been trying to get my JSON schema right. I have a boolean
property based on which I have to determine the required properties. Below is my sample JSON
which I want to fail the validation with item3
not present.
{
"item1": true,
"item2": "ABC"
}
这是我要验证通过的JSON
This is the JSON which I want the validation to pass
{
"item1": true,
"item2": "ABC",
"item3": {
"subItem1": "ABC",
"subItem2": "BAC"
}
}
类似地,如果item1
是false
,则上述两个JSON的验证都应通过.
Similarly, if the item1
is false
, then the validation should pass for both the above JSON's.
我的JSON模式如下.
My JSON schema for the same is as below.
{
"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
为true,则应为subItem2
.
如果item1
为false,则不需要item3
,但仍应进行验证(如果包含).
If item1
is true, subItem2
should be required.
If item1
is false, then item3
is not required, but should still validate if included.
推荐答案
您的if
/then
/else
块在验证方面可以正常工作.
Your if
/then
/else
block works correctly in terms of validation.
您提供的希望传递的示例JSON失败,因为您要求item3
的属性必须为subItem1
和subItem2
,但没有.
The example JSON you provided that you expect to pass, fails, because you have required that item3
has a property of subItem1
and subItem2
, but it does not.
现在,您已经更新了示例JSON,该示例应传递给包含subItem1
和subItem2
的正确的item3
,验证将通过您提供的模式传递.
Now you've updated your example JSON that should pass to correct item3
containing subItem1
and subItem2
, the validation passes with the schema you've provided.
此外,如果我理解正确,则需要:
Additionally, you want, If I understand correctly:
如果item1为true,则应该要求subItem2.如果item1为假,则 item3不是必需的,但如果包含的话,仍应进行验证.
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
)
Move the schema that makes subItem3
required from item3
to your then
clause. This will make it so subItem3
is only "required" if your if
schema validates successfully (item1
is true
)
{
"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模式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!