[英]JSON Schema if/then require nested object
我有一个JSON:
{
"i0": {
"j0": {
"a0": true
}
},
"i1": {
"j1": {
"a1": "stuff"
}
}
}
我要验证:如果a0
为true,则必须为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
或boolean
时,架构可以通过验证。
required
关键字可确保if
/ then
子方案仅与实际上包含i0.j0.a0
/ i1.j1.a1
属性路径的对象匹配。
另外, a0
/`a1属性的required
关键字仅指定它们存在 。 如果需要,可以向该子模式添加更多验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.