【问题标题】:Validate JSON Schema based on "type" property根据“type”属性验证 JSON Schema
【发布时间】:2019-11-25 23:35:39
【问题描述】:

我有以下 JSON 验证我当前的架构:

    {
      "information": [
        {
          "value": "closed",
          "type": "power"
        },
        {
          "value": "on",
          "type": "door"
        }
      ]
    }

但是,我希望此验证失败(因为 open/closed 应与 door 相关,而 on/off 应仅与 power 相关。

如何将两者联系起来?

我当前的工作模式:

  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "definitions": {
    "Type": {
      "type": "string",
      "enum": ["power", "door"]
    },
    "Power": {
      "type": "string",
      "enum": ["on", "off"]
    },
    "Door": {
      "type": "string",
      "enum": ["open", "closed"]
    },
    "InformationField": {
      "type": "object",
      "required": [ "type", "value" ],
      "properties": {
        "label": { "type": "string" },
        "value": {
          "anyOf": [
            { "$ref": "#/definitions/Power"},
            { "$ref": "#/definitions/Door"}
          ]
        },
        "type": { "$ref": "#/definitions/Type" }
      }
    },
    "Information": {
      "type": "array",
      "items": { "$ref": "#/definitions/InformationField" }
    },
  },
  "properties": {
    "information": { "$ref": "#/definitions/Information" }
  },
}

我有很多类型,所以我想以最简洁的方式来做这件事。

这是我的尝试之一,但无法正确验证:

{ 
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "definitions": {
    "Type": {
      "type": "string",
      "enum": ["power", "door"]
    },
    "Power": {
      "type": "string",
      "enum": ["on", "off"]
    },
    "Door": {
      "type": "string",
      "enum": ["open", "closed"]
    },
    "InformationField": {
      "type": "object",
      "required": [ "label", "value" ],
      "properties": {
        "label": { "type": "string" },
        "type": { "$ref": "#/definitions/Type" }
      },
      "anyOf": [
        {
          "if": {
           "properties": { "type": { "const": "power" } }
          },
          "then": {
            "properties": { "value": { "$ref": "#/definitions/Power" } }
          }
        },
        {
          "if": {
            "properties": { "type": { "const": "door" } }
          },
          "then": {
            "properties": { "value": { "$ref": "#/definitions/Door" } }
          }
        }
      ]
    },
    "Information": {
      "type": "array",
      "items": { "$ref": "#/definitions/InformationField" }
    },
  },
  "properties": {
    "information": { "$ref": "#/definitions/Information" }
  },
}

(验证,即使它不应该......)

【问题讨论】:

    标签: json validation jsonschema


    【解决方案1】:

    在我的尝试中将anyOf 更改为allOf,从而修复了验证问题。

    我的理由是:

    来自anyOf JSON 模式规范:

    5.5.4.2。成功验证的条件

    如果一个实例成功地验证了至少一个由该关键字的值定义的架构,则该实例成功地验证了该关键字。

    如果我的第一个if 条件为假,那么它不会评估then因此是有效的

    使用allOf 评估每个条件的验证(而不是条件本身)。

    【讨论】:

    • 另外,您可以将 else: false 添加到您的 if/then 架构中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 2018-03-25
    • 1970-01-01
    • 2020-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多