【发布时间】:2020-10-18 17:05:54
【问题描述】:
我如何检查属性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"
}
]
}
【问题讨论】:
标签: json jsonschema json-schema-validator