JSON 架构 if/then 需要嵌套对象
JSON Schema if/then require nested object
我有一个JSON:
{
"i0": {
"j0": {
"a0": true
}
},
"i1": {
"j1": {
"a1": "stuff"
}
}
}
我要验证:
如果 a0
为真,则需要 a1
。
我的架构目前是:
{
"$schema": "http://json-schema.org/draft-07/schema",
"id": "id",
"type": "object",
"required": [
"i0",
"i1"
],
"allOf": [
{
"if": {
"properties": {
"i0": {
"j0": {
"a0": true
}
}
}
},
"then": {
"properties": {
"i1": {
"j1": {
"required": [
"a1"
]
}
}
}
}
}
]
}
这个条件似乎不是 运行。或者,如果我尝试将 required
置于与我正在检查的值相同的级别,我已经看到有一个非常相似的条件可以工作。如:
"allOf": [
{
"if": {
"a0": true
},
"then": {
"required": [
"a1"
]
}
}
]
但这需要 a1
与 a1
一起低于 j0
。
如何根据 j0
下的值要求 j1
下的对象?
试试这个:
{
"if":{
"type":"object",
"properties":{
"i0":{
"type":"object",
"properties":{
"j0":{
"type":"object",
"required":["a0"]
}
},
"required":["j0"]
}
},
"required":["i0"]
},
"then":{
"type":"object",
"properties":{
"i1":{
"type":"object",
"properties":{
"j1":{
"type":"object",
"required":["a1"]
}
},
"required":["j1"]
}
},
"required":["i1"]
}
}
您必须在 if
/then
关键字中使用整体结构,从它们具有的任何公共根开始。在这种情况下,它们的路径在 i0
/i1
属性 处开始分叉,因此您必须从该点开始包括整个结构。
type
关键字确保您拥有一个对象,否则当使用其他类型(例如 string
s 或 boolean
s)时,架构可能会通过验证。
required
关键字确保 if
/then
子模式仅匹配实际包含 i0.j0.a0
/i1.j1.a1
[=37= 的对象] 路径,分别。
此外,a0
/`a1 属性的 required
关键字仅指定它们 存在 。如果需要,您可以向该子模式添加更多验证。
我有一个JSON:
{
"i0": {
"j0": {
"a0": true
}
},
"i1": {
"j1": {
"a1": "stuff"
}
}
}
我要验证:
如果 a0
为真,则需要 a1
。
我的架构目前是:
{
"$schema": "http://json-schema.org/draft-07/schema",
"id": "id",
"type": "object",
"required": [
"i0",
"i1"
],
"allOf": [
{
"if": {
"properties": {
"i0": {
"j0": {
"a0": true
}
}
}
},
"then": {
"properties": {
"i1": {
"j1": {
"required": [
"a1"
]
}
}
}
}
}
]
}
这个条件似乎不是 运行。或者,如果我尝试将 required
置于与我正在检查的值相同的级别,我已经看到有一个非常相似的条件可以工作。如:
"allOf": [
{
"if": {
"a0": true
},
"then": {
"required": [
"a1"
]
}
}
]
但这需要 a1
与 a1
一起低于 j0
。
如何根据 j0
下的值要求 j1
下的对象?
试试这个:
{
"if":{
"type":"object",
"properties":{
"i0":{
"type":"object",
"properties":{
"j0":{
"type":"object",
"required":["a0"]
}
},
"required":["j0"]
}
},
"required":["i0"]
},
"then":{
"type":"object",
"properties":{
"i1":{
"type":"object",
"properties":{
"j1":{
"type":"object",
"required":["a1"]
}
},
"required":["j1"]
}
},
"required":["i1"]
}
}
您必须在 if
/then
关键字中使用整体结构,从它们具有的任何公共根开始。在这种情况下,它们的路径在 i0
/i1
属性 处开始分叉,因此您必须从该点开始包括整个结构。
type
关键字确保您拥有一个对象,否则当使用其他类型(例如 string
s 或 boolean
s)时,架构可能会通过验证。
required
关键字确保 if
/then
子模式仅匹配实际包含 i0.j0.a0
/i1.j1.a1
[=37= 的对象] 路径,分别。
此外,a0
/`a1 属性的 required
关键字仅指定它们 存在 。如果需要,您可以向该子模式添加更多验证。