JSONSchema-必需属性取决于父属性 [英] JSONSchema - Required property dependent on parent property
问题描述
我想根据根模式中某个属性的存在,在数组子模式中应用一个附加的必需属性。我的模式设置如下:
I would like to apply an additional "required" property in an array sub schema based on the presence of a property in the root schema. I have my schema set like this:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required":[
"isParentDependency",
"subArray"
],
"properties": {
"isParentDependency": {
"$id": "#/properties/isParentDependency",
"type": "boolean"
},
"subArray": {
"$id": "#/properties/subArray",
"type": "array",
"items": {
"$id": "#/properties/subArrayItem",
"required": ["alwaysRequiredProp"],
"dependencies": {
"isParentDependency":{
"required":["requiredPropIfIsParentDependency"]
}
},
"properties": {
"alwaysRequiredProp": {
"$id": "#/properties/subArray/items/properties/alwaysRequiredProp",
"type": "boolean"
},
"requiredPropIfIsParentDependency": {
"$id": "#/properties/subArray/items/properties/requiredPropIfIsParentDependency",
"type": "boolean"
}
}
}
}
}
}
通行证
Passing Cases
{
"isParentDependency": false,
"subArray": [{
"alwaysRequiredProp": true
}]
}
"isParentDependency": true,
"subArray": [{
"alwaysRequiredProp": true,
"requiredPropIfIsParentDependency":true
}]
}
失败案例
Failing Cases
{
"isParentDependency": true,
"subArray": [{
"alwaysRequiredProp": true
}]
}
很显然,这是行不通的,但是我一直无法弄清楚如何创建指向根模式的指针(或应用带有$ ref的if / else类型的解决方案)
Clearly this is not going to work but I have been unable to figure out how to make a pointer to the root schema (or apply an if/else type solution with a $ref)
非常感谢任何指导!
推荐答案
使用JSON Schema,每个嵌套级 properties
无法看到树。因此,您必须在需要查看的最顶层定义条件。在这种情况下,这就是根级别。
With JSON Schema, each level of nested properties
down the tree cannot see up the tree. Therefore you have to define your conditions at the top most level to which it needs to see. In this case, that's the root level.
看看这个模式。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"definitions": {
"subArray": {
"type": "array",
"items": {
"required": [
"alwaysRequiredProp"
],
"properties": {
"alwaysRequiredProp": {
"type": "boolean"
},
"requiredPropIfIsParentDependency": {
"type": "boolean"
}
}
}
}
},
"required": [
"isParentDependency",
"subArray"
],
"properties": {
"isParentDependency": {
"type": "boolean"
},
"subArray": {
"$ref": "#/definitions/subArray"
}
},
"if": {
"properties": {
"isParentDependency": {
"const": true
}
}
},
"then": {
"properties": {
"subArray": {
"items": {
"required": [
"requiredPropIfIsParentDependency"
]
}
}
}
}
}
subArray
已移至一个定义,然后引用它,因此可以更容易地看到条件应用程序逻辑。
subArray
has been moved to a definition and then referenced, so the conditional application logic can be seen easier.
subArray
的定义只能确定项目结构的属性(如果已提供),并且需要 alwaysRequiredProp
。不需要 requiredPropIfIsParentDependency
。
The definition of subArray
only determines the properties of the items structure if they are provided, and requires alwaysRequiredProp
. It does not require requiredPropIfIsParentDependency
.
条件应用程序使用 if
和然后是
关键字。
The conditional application comes in using the if
and then
keywords.
如果 if
模式为true,然后应用 else
模式。
If the if
schema is true, then the else
schema is applied.
if
是架构。适用于根级别。它要求 isParentDependency
为 true
(它不需要属性本身,因为总是需要的。在内部需要该属性if语句并不意味着始终需要该属性,除非 else
为 false
,但我们甚至没有使用 else
此处。)
The value of if
is a schema. It is applicable to the root level. It requires isParentDependency
be true
(it does not require the property itself, because that is ALWAYS required. Requiring the property inside the if statement would NOT mean the property is always required, unless else
was false
, but we're not even using else
here).
当 isParentDependency
为 true
,则应用然后
模式,该模式将树向下看至 subArray
的项目,需要 requiredPropIfIsParentDependency
。
When isParentDependency
is true
, the then
schema is applied, which looks down the tree to subArray
's items, requiring requiredPropIfIsParentDependency
.
您可以使用 jsonschema.dev (预加载有此架构和你失败实例)。
You can test this working in browser using jsonschema.dev (preloaded with this schema and your fail instance).
这篇关于JSONSchema-必需属性取决于父属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!