JSON Schema Draft 7在验证数组对象中的必要属性时出现的问题。

问题描述 投票:0回答:1

我有这样一个JSON Schema文件(缩小了以避免显示与问题无关的东西)。

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "$id": "urn:jsonschema:testing:frmwk",
  "type": "object",
  "title": "The root schema",
  "default": {},
  "required": [
    "processOrder"
  ],
  "additionalProperties": true,
  "properties": {
    "processOrder": {
      "$id": "#processOrder",
      "type": "array",
      "title": "The processOrder schema",
      "default": [],
      "additionalItems": true,
      "items": {
        "anyOf": [
          {
            "$id": "#0",
            "type": "object",
            "title": "The first anyOf schema",
            "default": {},
            "additionalProperties": true,
            "properties": {
              "cleanHistory": {
                "$id": "#cleanHistory",
                "type": "object",
                "title": "The cleanHistory schema",
                "default": {},
                "additionalProperties": true,
                "properties": {}
              }
            }
          },
          {
            "$id": "#3",
            "type": "object",
            "title": "The fourth anyOf schema",
            "default": {},
            "additionalProperties": true,
            "properties": {
              "processEvents": {
                "$id": "#processEvents",
                "type": "object",
                "title": "The processEvents schema",
                "default": {},
                "required": [
                  "identityTrigger"
                ],
                "additionalProperties": true,
                "properties": {
                  "identityTrigger": {
                    "$id": "#identityTrigger",
                    "type": "string",
                    "title": "The identityTrigger schema",
                    "default": ""
                  }
                }
              }
            }
          }
        ],
        "$id": "#items"
      }
    }
  }
}

我想验证的JSON是:

{
  "description": "description",
  "requesteeName": "05300005",
  "processOrder": [
  {"cleanHistory": {} },
    {"processEvents": {
      "identityTrigger": "some trigger"
    }}
  ],
  "deleteObjects": "true"
}

现在,我希望当我删除这个字符串时,它会失败。

"identityTrigger": "some trigger"

因为 "identityTrigger" 属性在processEvents对象中的所需数组中,但没有失败,一定是数组匹配(processOrder数组)出了问题。但是没有失败,一定是数组匹配(processOrder数组)出了问题。谁能给我一个建议呢,谢谢

json jsonschema json-schema-validator
1个回答
2
投票

验证仍然成功的原因是,第一个 anyOf 选项没有指定任何 required 属性,同时允许任何 additionalProperties因此,一个空的JSON对象总是对第一个选项有效。anyOf 以避免空对象对其中任何一个有效。

然而,这里似乎还有一些需要澄清的地方。

  1. 术语 "空 "是指 "空",而不是 "空"。additionalProperties 关键字默认为 true 而在这种情况下不需要提及。根据 json-schema.org:

    负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: additionalProperties 关键字用于控制对额外的东西的处理,也就是其名称未在 properties 关键字。默认情况下,允许任何附加属性。

  2. additionalItems 关键字只适用于 items 是一个数组(即数组在特定位置上是一个不同类型的元组)。根据 json-schema.org:

    时间 items 是一个单一的模式,该 additionalItems 关键字是没有意义的,它不应该被使用。

  3. 你的 default 的值是空对象,这对描述的结构来说是无效的。required 属性。最好把这些 default 的值也被删除了--除非这只是因为您在这个问题的范围内删除了它们的内容,以保持简单。

0
投票

我已经更好地理解了它是如何工作的,就像 @Carten 在评论中说的那样。任何 单项不需要属性的情况下,无法工作,因为任何属性的项目都会被验证,正确的模式应该是(粗体的变化)。

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "$id": "urn:jsonschema:testing:frmwk",
  "type": "object",
  "title": "The root schema",
  "default": {},
  "required": [
    "processOrder"
  ],
  "additionalProperties": true,
  "properties": {
    "processOrder": {
      "$id": "#processOrder",
      "type": "array",
      "title": "The processOrder schema",
      "default": [],
      "additionalItems": false,
      "items": {
        "anyOf": [
          {
            "$id": "#0",
            "type": "object",
            "title": "The first anyOf schema",
            "default": {},
            "additionalProperties": true,
            "required": ["cleanHistory"],
            "properties": {
              "cleanHistory": {
                "$id": "#cleanHistory",
                "type": "object",
                "title": "The cleanHistory schema",
                "default": {},
                "additionalProperties": true,
                "properties": {}
              }
            }
          },
          {
            "$id": "#3",
            "type": "object",
            "title": "The fourth anyOf schema",
            "default": {},
            "additionalProperties": true,
            "required": ["processEvents"],
            "properties": {
              "processEvents": {
                "$id": "#processEvents",
                "type": "object",
                "title": "The processEvents schema",
                "default": {},
                "required": [
                  "identityTrigger"
                ],
                "additionalProperties": true,
                "properties": {
                  "identityTrigger": {
                    "$id": "#identityTrigger",
                    "type": "string",
                    "title": "The identityTrigger schema",
                    "default": ""
                  }
                }
              }
            }
          }
        ],
        "$id": "#items"
      }
    }
  }
}

主要的变化是 2:

  1. 增加了 "additionalItems": false 阵列对象定义中的
  2. 新增 "所需"。["cleanHistory"]"所需"。["processEvents"] 对于每个对应的根属性项,在我的例子中("cleanHistory "和 "processEvents"),通过这种方式,AnyOf将强制使用列出的其中一个模式来验证数组中的项目。
© www.soinside.com 2019 - 2024. All rights reserved.