JSON 架构草案 07 if then 条件验证语句

JSON Schema draft 07 if then statements for conditional validation

我正在尝试创建一个结构根据值略有不同的模式,并决定使用 draft 07 和 ajv 来验证它。 我要创建的结构如下 -

   "url":{
   "some-random-string":{
            "pattern":"somevalue",
            "handler":"one-of-a-few-allowed-values",
            "kwargs":{ "conditional object" with further constraints}
             }
   }

其中,模式是必需的,某些 kwargs 对象将具有其他必需的键。我尝试使用一系列 if..then 语句结合参考如下:

   "url": {
  "type": "object",
  "patternProperties": {
    "^.*$": {
      "properties": {
        "pattern": {
          "type": "string"
        },
        "handler": {
          "type": "string",
          "enum": ["val1","val2"...
          ]
        }
      },
      "required": ["pattern"],
       "if": {
          "properties": {
            "handler": {
              "enum": ["val1"]
            }
          }
        },
        "then": {
          "properties": {
            "kwargs": {
              "$ref": "#/definitions/val1"
            }
          }
        },
        "if": {
          "properties": {
            "handler": {
              "enum": ["val2"]
            }
          }
        },
        "then": {
          "properties": {
            "kwargs": {
              "$ref": "#/definitions/val2"
            },"required":["function"]
          }
        },

所需的模式约束有效,所需的函数约束无效。

我什至尝试将所有 if-then 语句包装到一个 allOf 数组中,每组 if-then 都在一个对象中,但它似乎不起作用。

参考目前看起来像这样

     "val2": {
  "type": ["object", "boolean"],
  "properties": {
    "kwargs": {
      "type": "object",
      "properties": {
        "function": {
          "type": "string"
        },
        "methods": {
          "type": "array",
          "items": {
            "enum": ["GET", "PUT", "POST", "DELETE", "OPTIONS"]
          }
        }
      }
    }
  }
}

此架构使用 if 检查数据中是否存在 handlerthen 它使用 anyOf 中的 const 检查 handler 值] 上下文。

{
  "properties": {
    "url": {
      "type": "object",
      "patternProperties": {
        "^.*$": {
          "properties": {
            "pattern": {"type": "string"},
            "handler": {
              "type": "string",
              "enum": ["val1", "val2"]
            }
          },
          "required": ["pattern"],
          "if": {"required": ["handler"]},
          "then": {
            "anyOf": [
              {
                "properties": {
                  "handler": {"const": "val1"},
                  "kwargs": {"$ref": "#/definitions/val1"}
                }
              },
              {
                "properties": {
                  "handler": {"const": "val2"},
                  "kwargs": {"$ref": "#/definitions/val2"}
                }
              }
            ]
          }
        }
      }
    }
  },
  "definitions": {
    "val1": {
      "type": "string"
    },
    "val2": {
      "type": "integer"
    }
  }
}