繁体   English   中英

JSON模式中的`additionalProperties`规则不适用于嵌套级别属性

[英]`additionalProperties` rule in JSON schema is not applied to nested level properties

因此,我有一个JSON模式,其中additionalProperties规则设置为false

{
  "type": "object",
  "properties": {
    "metadata": {
      "type": "object",
      "properties": {
        "a": {
          "type": "string"
        },
        "b": {
          "type": "string"
        },
        "c": {
          "type": "string"
        }
      }
    },
    "street_type": {
      "type": "string",
      "enum": [
        "Street",
        "Avenue",
        "Boulevard"
      ]
    }
  },
  "additionalProperties": false
}

和像

{
  "metadata": {
    "a": "aa",
    "b": "bb",
    "c": "cc",
    "d": "dd"
  }
}

我希望我的JSON解析器模式/验证通过验证,我使用JSON解析器模式com.github.fge.jsonschema.main.JsonSchema通过验证,虽然metadata/d没有出现在架构与additionalProperties设置为false

这是非常令人误解的,有人可以将我引导到正确的方向。

additionalProperties属性JSON模式定义是否仅适用于顶级字段,而不适用于任何嵌套级字段?

附加属性JSON模式定义是否仅适用于顶级字段,而不适用于任何嵌套级字段?

不,只要它位于描述对象的架构中,就应该将其置于所需的任何级别。 就您而言,您只需将其放在错误的位置即可。 这应该工作:

{
  "type": "object",
  "properties": {
    "metadata": {
      "type": "object",
      "properties": {
        "a": {
          "type": "string"
        },
        "b": {
          "type": "string"
        },
        "c": {
          "type": "string"
        }
      },
      "additionalProperties": false
    },
    "street_type": {
      "type": "string",
      "enum": [
        "Street",
        "Avenue",
        "Boulevard"
      ]
    }
  }
}

假设您要按原样验证以下对象:

{
  a: {
    b: {
      c: {
        d: 42
      }
    }
  }
}

一种有效的方案是:

{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "a": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "b": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "c": {
              "type": "object",
              "additionalProperties": false,
              "properties": {
                "d": {
                  "const": 42
                }
              }
            }
          }
        }
      }
    }
  }
}

上面的架构非常冗长,但此处仅用于说明目的。 通过使用$ref并将模式组合在一起,您应该能够使其更加简洁。

暂无
暂无

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

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