【问题标题】:JSON Schema draft 07 if then statements for conditional validationJSON Schema 草案 07 if then 用于条件验证的语句
【发布时间】:2018-05-25 05:15:08
【问题描述】:

我正在尝试创建一个根据值具有略微不同结构的架构,并决定使用草案 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"]
          }
        }
      }
    }
  }
}

【问题讨论】:

    标签: jsonschema ajv


    【解决方案1】:

    此架构使用if 来检查handler 是否存在于数据中,then 它使用constanyOf 上下文中检查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"
        }
      }
    }
    

    【讨论】:

    • 谢谢,这行得通,虽然我有 2 个问题,1. 我可以完全删除 if.. then 并为处理程序添加所需的约束吗? 2. 如果我需要对 val1 和 val2 属性有必要的约束,它们应该在定义中定义还是在 $ref 下方定义?
    • 1.是的,如果在requiredpattern 旁边)中添加handler,则可以删除ifthen。 2. 如果需要kwargs 或其兄弟,则必须在anyOf 项(与properties 相邻)中添加require。如果您需要在 kwargs 中添加某些内容,请在定义 (#/definitions/val1/required) 中添加 require
    • 非常感谢。我现在唯一的问题是我得到了所有与常量不匹配的值的错误,例如,如果我试图验证的 json 有一个名为 val2 的处理程序,我得到一个错误,说 properties.handler 应该匹配 const val1 的值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    • 2023-02-06
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多