繁体   English   中英

Json 架构如果/则未按预期工作

[英]Json Schema if/then not working as expected

我正在尝试验证一个简单的 json 架构,但我似乎无法做到这一点。 我正在尝试将这段代码实现到 JsonSchema 中,但它没有正确验证。

代码:

if (field1 is "REAL") {
    then both attributes.A and attributes.B are present
    if (attributes.field2 is "true") {
        then attributes.C is also present
    }
}

我已将其转换为 JsonSchema,如下所示:

{
  "$schema": "https://json-schema.org/draft/2019-09/schema#",
  "properties": {
    "field1": {},
    "attributes": {
      "type": "object",
      "properties": {
        "field2": {
          "type": "string",
          "default": "false"
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "field1": {
            "enum": [
              "REAL"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "attributes": {
            "$id": "#/properties/realAttributes",
            "type": "object",
            "title": "The Attributes Schema",
            "required": [
              "A",
              "B"
            ],
            "properties": {
              "A": {},
              "B": {},
              "C": {},
              "D": {},
              "field2": {}
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "field1": {
            "enum": ["REAL"]
          },
          "attributes": {
            "properties": {
              "field2": {
                "enum": ["true"]
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "attributes": {
            "required": [
              "C"
            ]
          }
        }
      }
    }
  ]
}

allOf中的第二个if验证无法正常工作,我不知道为什么。 当我通过field1而不是field2时,它失败了。 在这里玩失败的实现

我希望这会通过,但它报告一个错误,说C丢失。 失败案例:

{
  "field1": "REAL",
    "attributes": {
      "A": 4,
        "B": 5
    }
}

JSON 模式通过将模式(和子模式)应用于实例位置来工作。

条件关键字if/then/else通过将if的模式值应用到实例位置来工作,并根据结果应用thenelse if 提供。

这里的关键是 JSON Schema 是一种基于约束的语言,任何你没有指定/约束的东西都是允许的。

让我们采用您的if子模式,它没有按您的预期工作......

{
  "properties": {
    "field1": {
      "enum": ["REAL"]
    },
    "attributes": {
      "properties": {
        "field2": {
          "enum": ["true"]
        }
      }
    }
  }
}

让我们看一下您在此处指定的内容。 但首先,让我们检查一下properties的作用......

如果对于出现在两个
实例并作为此关键字值中的名称,孩子
该名称的实例成功验证
对应的架构。

https://tools.ietf.org/html/draft-handrews-json-schema-02#section-9.3.2.1

在你的if子模式中,你说过,“如果field1存在,它应该是“REAL” “AND”如果 object 有一个属性attributes ,并且如果 object 值有一个属性field2 ,那么它应该是"true" “ .

问题是,如果field1不存在,并且attributes不存在,那么子模式仍将通过验证。

如果您要求该财产存在,则必须require该财产。

让我们看看您的架构添加了required的关键字...

{
  "$schema": "https://json-schema.org/draft/2019-09/schema#",
  "properties": {
    "field1": {},
    "attributes": {
      "type": "object",
      "properties": {
        "field2": {
          "type": "string",
          "default": "false"
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "required": [
          "field1"
        ],
        "properties": {
          "field1": {
            "enum": [
              "REAL"
            ]
          }
        }
      },
      "then": {
        "properties": {
          "attributes": {
            "$id": "#/properties/realAttributes",
            "type": "object",
            "title": "The Attributes Schema",
            "required": [
              "A",
              "B"
            ],
            "properties": {
              "A": {},
              "B": {},
              "C": {},
              "D": {},
              "field2": {}
            }
          }
        }
      }
    },
    {
      "if": {
        "required": [
          "field1"
        ],
        "properties": {
          "field1": {
            "enum": [
              "REAL"
            ]
          },
          "attributes": {
            "required": [
              "field2"
            ],
            "properties": {
              "field2": {
                "enum": [
                  "true"
                ]
              }
            }
          }
        }
      },
      "then": {
        "properties": {
          "attributes": {
            "required": [
              "C"
            ]
          }
        }
      }
    }
  ]
}

游乐场: https://www.jsonschemavalidator.net/s/mAPFXugk

现在子模式也需要关键字,验证按您的预期工作。

如果您在使用 JSON 模式中的条件时遇到问题,您可以通过将子模式更改为false来测试您的假设,例如then的值。 您可以使用此技术来检查是否满足哪些条件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM