JSONSchema - 必需 属性 取决于父级 属性
JSONSchema - Required property dependent on parent property
我想根据根模式中 属性 的存在,在数组子模式中应用额外的 "required" 属性。我的架构设置如下:
{
"$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"
}
}
}
}
}
}
通过案例
{
"isParentDependency": false,
"subArray": [{
"alwaysRequiredProp": true
}]
}
"isParentDependency": true,
"subArray": [{
"alwaysRequiredProp": true,
"requiredPropIfIsParentDependency":true
}]
}
失败案例
{
"isParentDependency": true,
"subArray": [{
"alwaysRequiredProp": true
}]
}
显然这行不通,但我一直无法弄清楚如何制作指向根架构的指针(或应用带有 $ref 的 if/else 类型解决方案)
非常感谢任何指导!
使用 JSON 模式,嵌套的每一层 properties
树下看不到树上。因此,您必须在需要查看的最顶层定义您的条件。在这种情况下,这是根级别。
看看这个架构。
{
"$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
的定义只确定了项目结构的属性(如果提供的话),并且需要alwaysRequiredProp
。它不需要 requiredPropIfIsParentDependency
.
有条件的应用程序使用 if
和 then
关键字。
如果 if
模式为真,则应用 else
模式。
if
的值是一个模式。它适用于根级别。它要求 isParentDependency
是 true
(它不需要 属性 本身,因为它始终是必需的。在 if 语句中要求 属性 并不意味着 属性 总是需要的,除非 else
是 false
,但我们在这里甚至没有使用 else
)。
当 isParentDependency
为 true
时,将应用 then
架构,它会在树下查找 subArray
的项目,需要 requiredPropIfIsParentDependency
。
您可以使用 jsonschema.dev(预加载了此架构和您的失败实例)在浏览器中测试此功能。
我想根据根模式中 属性 的存在,在数组子模式中应用额外的 "required" 属性。我的架构设置如下:
{
"$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"
}
}
}
}
}
}
通过案例
{
"isParentDependency": false,
"subArray": [{
"alwaysRequiredProp": true
}]
}
"isParentDependency": true,
"subArray": [{
"alwaysRequiredProp": true,
"requiredPropIfIsParentDependency":true
}]
}
失败案例
{
"isParentDependency": true,
"subArray": [{
"alwaysRequiredProp": true
}]
}
显然这行不通,但我一直无法弄清楚如何制作指向根架构的指针(或应用带有 $ref 的 if/else 类型解决方案)
非常感谢任何指导!
使用 JSON 模式,嵌套的每一层 properties
树下看不到树上。因此,您必须在需要查看的最顶层定义您的条件。在这种情况下,这是根级别。
看看这个架构。
{
"$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
的定义只确定了项目结构的属性(如果提供的话),并且需要alwaysRequiredProp
。它不需要 requiredPropIfIsParentDependency
.
有条件的应用程序使用 if
和 then
关键字。
如果 if
模式为真,则应用 else
模式。
if
的值是一个模式。它适用于根级别。它要求 isParentDependency
是 true
(它不需要 属性 本身,因为它始终是必需的。在 if 语句中要求 属性 并不意味着 属性 总是需要的,除非 else
是 false
,但我们在这里甚至没有使用 else
)。
当 isParentDependency
为 true
时,将应用 then
架构,它会在树下查找 subArray
的项目,需要 requiredPropIfIsParentDependency
。
您可以使用 jsonschema.dev(预加载了此架构和您的失败实例)在浏览器中测试此功能。