在 JSON 模式中有条件地检查

Conditional check in JSON schema

我有以下 JSON 架构:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "Payload": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "Person": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "additionalProperties": false,
                        "properties": {
                            "Id": {
                                "type": "string"
                            },
                            "Name": {
                                "type": "string"
                            }
                        },
                        "required": [
                            "Id",
                            "Name"
                        ]
                    }
                }
            }
        },
        "Reference": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "Status": {
                    "anyOf": [
                        {
                            "Passed": {
                                "type": "string"
                            },
                            "Failed": {
                                "type": "string"
                            }
                        }
                    ]
                }
            }
        }
    },
    "anyOf": [
        {
            "additionalProperties": false,
            "properties": {
                "Status": {
                    "type": "string",
                    "enum": [
                        "Failed"
                    ]
                }
            },
            "required": [
                "Reference"
            ],
            "not": {
                "required": [
                    "Payload"
                ]
            }
        },
        {
            "additionalProperties": true,
            "properties": {
                "Status": {
                    "type": "string",
                    "enum": [
                        "Passed"
                    ]
                }
            },
            "required": [
                "Reference"
            ]
        }
    ]
}

我想检查 JSON 消息的状态是否失败,那么人员数组不应存在。 它应该仅在状态通过时出现。

我尝试关注 但肯定是我做错了什么,因为验证器通过时状态为“失败”并且存在人员详细信息。谁能告诉我哪里做错了?

你有一些问题。

/properties/Reference/properties/Status

这不是一个有效的架构。您似乎在尝试描述一个枚举。

附加属性

原因很复杂,但条件模式不适用于 additionalProperties。好消息是它也没有必要。你可以把那些去掉。

/anyOf

看起来您正在使用 "Enum" 模式,但在这种情况下,暗示模式更好,因为只有一个枚举状态具有额外的约束。

以嵌套为条件 属性

您定义 Reference.Status 值的模式实际上只是指向 Status。您还需要一个描述父 属性 的架构。


以下是我认为您的模式正在尝试执行的操作。

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "Payload": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "Person": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "Id": { "type": "string" },
              "Name": { "type": "string" }
            },
            "required": ["Id", "Name"]
          }
        }
      }
    },
    "Reference": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "Status": { "enum": ["Passed", "Failed"] }
      }
    }
  },
  "anyOf": [
    {
      "not": {
        "properties": {
          "Reference": {
            "properties": {
              "Status": { "enum": ["Failed"] }
            },
            "required": ["Status"]
          }
        },
        "required": ["Reference"]
      }
    },
    { "not": { "required": ["Payload"] } }
  ]
}