JSON 架构:如何检查每个具有特定值的数组项是否具有所需的属性?
JSON Schema: How to check every array item that has a specific value has the required properties?
我如何检查 属性 field1
中具有值 Value1
的每个数组项,属性 field2
是必需的?
如果 field1
的值不是 Value1
,则只需要 field1
。
这是一个例子:
{
"property_abc":[
{
"field1":"Value1",
"field2": "Value2"
},
{
"field1":"Value2"
},
{
"field1":"Value3"
}
]
}
这是我的架构:
{
"$schema": "http://json-schema.org/draft-07/schema",
"additionalProperties": false,
"properties": {
"property_abc": {
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"enum": [
"Value1",
"Value2",
"Value3"
],
"type": "string"
},
"field2": {
"enum": [
"Value1",
"Value2",
"Value3"
],
"type": "string"
}
},
"allOf": [
{
"if": {
"properties": {
"property_abc": {
"items": {
"properties": {
"field1": {
"const": "Value1"
}
}
}
}
}
},
"then": {
"required": [
"field1",
"field2"
]
},
"else": {
"required": [
"field1"
]
}
}
]
}
},
"property_xyz": {
"type": "number"
}
},
"type": "object"
}
上面的例子是正确的。
但下面的会抛出错误,因为对于 property_abc
中的第一项,属性 field2
是必需的,但不存在:
{
"property_abc":[
{
"field1":"Value1"
},
{
"field1":"Value2"
},
{
"field1":"Value3"
}
]
}
您的 if
模式在错误的位置查找 - 此模式适用于 property_abc 数组值内的对象。我在下面粘贴了更正,并将其移到了 allOf
之外,这在这里没有任何用处。
您也可以查看 dependencies
关键字,它可能会有所帮助,但需要进行一些重构才能表达您的约束。
{
"$schema": "http://json-schema.org/draft-07/schema",
"additionalProperties": false,
"properties": {
"property_abc": {
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"enum": [
"Value1",
"Value2",
"Value3"
],
"type": "string"
},
"field2": {
"enum": [
"Value1",
"Value2",
"Value3"
],
"type": "string"
}
},
"if": {
"properties": {
"field1": {
"const": "Value1"
}
}
},
"then": {
"required": [
"field1",
"field2"
]
},
"else": {
"required": [
"field1"
]
}
}
},
"property_xyz": {
"type": "number"
}
},
"type": "object"
}
我如何检查 属性 field1
中具有值 Value1
的每个数组项,属性 field2
是必需的?
如果 field1
的值不是 Value1
,则只需要 field1
。
这是一个例子:
{
"property_abc":[
{
"field1":"Value1",
"field2": "Value2"
},
{
"field1":"Value2"
},
{
"field1":"Value3"
}
]
}
这是我的架构:
{
"$schema": "http://json-schema.org/draft-07/schema",
"additionalProperties": false,
"properties": {
"property_abc": {
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"enum": [
"Value1",
"Value2",
"Value3"
],
"type": "string"
},
"field2": {
"enum": [
"Value1",
"Value2",
"Value3"
],
"type": "string"
}
},
"allOf": [
{
"if": {
"properties": {
"property_abc": {
"items": {
"properties": {
"field1": {
"const": "Value1"
}
}
}
}
}
},
"then": {
"required": [
"field1",
"field2"
]
},
"else": {
"required": [
"field1"
]
}
}
]
}
},
"property_xyz": {
"type": "number"
}
},
"type": "object"
}
上面的例子是正确的。
但下面的会抛出错误,因为对于 property_abc
中的第一项,属性 field2
是必需的,但不存在:
{
"property_abc":[
{
"field1":"Value1"
},
{
"field1":"Value2"
},
{
"field1":"Value3"
}
]
}
您的 if
模式在错误的位置查找 - 此模式适用于 property_abc 数组值内的对象。我在下面粘贴了更正,并将其移到了 allOf
之外,这在这里没有任何用处。
您也可以查看 dependencies
关键字,它可能会有所帮助,但需要进行一些重构才能表达您的约束。
{
"$schema": "http://json-schema.org/draft-07/schema",
"additionalProperties": false,
"properties": {
"property_abc": {
"type": "array",
"items": {
"type": "object",
"properties": {
"field1": {
"enum": [
"Value1",
"Value2",
"Value3"
],
"type": "string"
},
"field2": {
"enum": [
"Value1",
"Value2",
"Value3"
],
"type": "string"
}
},
"if": {
"properties": {
"field1": {
"const": "Value1"
}
}
},
"then": {
"required": [
"field1",
"field2"
]
},
"else": {
"required": [
"field1"
]
}
}
},
"property_xyz": {
"type": "number"
}
},
"type": "object"
}