JSON 基于值的模式依赖

JSON Schema Dependency based on value

我需要创建一个架构,如果另一个 属性 具有特定值,则该架构期望 属性 存在。

{"handleFailure":"redirect","redirectUrl":"http://something.com"}

{"handleFailure":"reject"}

应该都有效,但是

{"handleFailure:"redirect"}

应该无效,因为 redirectUrl 属性 不存在。

我已经尝试使用像这样的两个模式制作顶级oneOf

{
  "type": "object",
  "additionalProperties": false,
  "oneOf": [
    {
      "properties": {
        "handleFailure": {
          "type": "string",
          "enum": [
            "redirect"
          ]
        },
        "redirectUrl": {
          "type": "string",
          "format": "uri"
        }
      }
    },
    {
      "properties": {
        "handleFailure": {
          "type": "string",
          "enum": [
            "reject"
          ]
        }
      }
    }
  ]
}

但我收到有关未定义属性的错误。 有办法吗?

"additionalProperties": false 标志插入子模式以防止那些对象具有额外的属性。

{
  "type": "object",
  "additionalProperties": false,
  "oneOf": [
    {
      "additionalProperties": false,
      "properties": {
        "handleFailure": {
          "type": "string",
          "enum": [
            "redirect"
          ]
        },
        "redirectUrl": {
          "type": "string",
          "format": "uri"
        }
      }
    },
    {
      "additionalProperties": false,
      "properties": {
        "handleFailure": {
          "type": "string",
          "enum": [
            "reject"
          ]
        }
      }
    }
  ]
}