如何使用数组元素执行 JSON 架构多级条件验证
How to do JSON Schema multi level conditional Validation with Array Elements
我正在处理一个 JSON 模式,但我被困在数组验证上。我有这个例子输入 JSON
{
"paths_1": {
"path_1": [
{
"abc": "valid_abc"
},
{
"abc": "invalid_abc"
}
]
},
"paths_2": {
"path_2": [
{
"ghi": "valid_ghi"
}
]
}
}
我对这个 JSON 数据的规则是,如果 paths_2.path_2[].ghi
是“valid_ghi”并且 paths_1.path_1[].abc
是“valid_abc”,那么需要“def " 具有“valid_abc”的对象的键。
我为此规则创建了这个 JSON 架构,但它没有按预期工作。
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"paths_1": {
"type": "object",
"properties": {
"items": {
"properties": {
"path_1": {
"properties": {
"abc": {
"type": "string"
},
"def": {
"type": "string"
}
}
}
}
}
}
},
"paths_2": {
"type": "object",
"properties": {
"items": {
"properties": {
"path_2": {
"properties": {
"ghi": {
"type": "string"
}
}
}
}
}
}
}
},
"allOf": [
{
"if": {
"allOf": [
{
"properties": {
"paths_1": {
"properties": {
"path_1": {
"contains": {
"properties": {
"abc": {
"const": "valid_abc"
}
},
"required": [
"abc"
]
}
}
}
}
}
},
{
"properties": {
"paths_2": {
"properties": {
"path_2": {
"contains": {
"properties": {
"ghi": {
"const": "valid_ghi"
}
},
"required": [
"ghi"
]
}
}
}
}
}
}
]
},
"then": {
"properties": {
"paths_1": {
"properties": {
"path_1": {
"items": {
"required": [
"def"
]
}
}
}
}
}
}
}
]
}
当我测试这个模式时,它 returns 'def' 是带有“invalid_abc”的对象所必需的 属性,而实际上它不应该。
我尝试将 contains 键更改为 JSON 模式中的 items 但在这种情况下,如果部分变为假并且验证器returns 输入有效。
有没有办法用给定的规则验证这个输入?
您需要再次检查 items
中的 valid_abc
。
您的 then
子句没有上下文,我认为这正是您所期望的。
"items": {
"if": {
"properties": {
"abc": {
"const": "valid_abc"
}
},
"required": [
"abc"
]
},
"then": {
"required": [
"def"
]
}
}
演示:https://jsonschema.dev/s/M3cvJ
因此,您可以简化条件检查,因为您不需要检查数组是否包含带有 valid_abc
的对象。您可以删除 if/allOf[0]
,然后展开 allOf
,因为这样它就只有一个子模式。
我正在处理一个 JSON 模式,但我被困在数组验证上。我有这个例子输入 JSON
{
"paths_1": {
"path_1": [
{
"abc": "valid_abc"
},
{
"abc": "invalid_abc"
}
]
},
"paths_2": {
"path_2": [
{
"ghi": "valid_ghi"
}
]
}
}
我对这个 JSON 数据的规则是,如果 paths_2.path_2[].ghi
是“valid_ghi”并且 paths_1.path_1[].abc
是“valid_abc”,那么需要“def " 具有“valid_abc”的对象的键。
我为此规则创建了这个 JSON 架构,但它没有按预期工作。
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"paths_1": {
"type": "object",
"properties": {
"items": {
"properties": {
"path_1": {
"properties": {
"abc": {
"type": "string"
},
"def": {
"type": "string"
}
}
}
}
}
}
},
"paths_2": {
"type": "object",
"properties": {
"items": {
"properties": {
"path_2": {
"properties": {
"ghi": {
"type": "string"
}
}
}
}
}
}
}
},
"allOf": [
{
"if": {
"allOf": [
{
"properties": {
"paths_1": {
"properties": {
"path_1": {
"contains": {
"properties": {
"abc": {
"const": "valid_abc"
}
},
"required": [
"abc"
]
}
}
}
}
}
},
{
"properties": {
"paths_2": {
"properties": {
"path_2": {
"contains": {
"properties": {
"ghi": {
"const": "valid_ghi"
}
},
"required": [
"ghi"
]
}
}
}
}
}
}
]
},
"then": {
"properties": {
"paths_1": {
"properties": {
"path_1": {
"items": {
"required": [
"def"
]
}
}
}
}
}
}
}
]
}
当我测试这个模式时,它 returns 'def' 是带有“invalid_abc”的对象所必需的 属性,而实际上它不应该。
我尝试将 contains 键更改为 JSON 模式中的 items 但在这种情况下,如果部分变为假并且验证器returns 输入有效。
有没有办法用给定的规则验证这个输入?
您需要再次检查 items
中的 valid_abc
。
您的 then
子句没有上下文,我认为这正是您所期望的。
"items": {
"if": {
"properties": {
"abc": {
"const": "valid_abc"
}
},
"required": [
"abc"
]
},
"then": {
"required": [
"def"
]
}
}
演示:https://jsonschema.dev/s/M3cvJ
因此,您可以简化条件检查,因为您不需要检查数组是否包含带有 valid_abc
的对象。您可以删除 if/allOf[0]
,然后展开 allOf
,因为这样它就只有一个子模式。