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

Nav*_*tha 3 validation schema json jsonschema json-schema-validator

所以我有一个 JSON 模式,其additionalProperties规则设置为falselike。

{
  "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
}
Run Code Online (Sandbox Code Playgroud)

和一个像这样的有效负载

{
  "metadata": {
    "a": "aa",
    "b": "bb",
    "c": "cc",
    "d": "dd"
  }
}
Run Code Online (Sandbox Code Playgroud)

我是否应该期望我的 JSON 模式解析器/验证器通过验证,我正在使用的 JSON 模式解析器会通过com.github.fge.jsonschema.main.JsonSchema验证,但在设置为 的metadata/d模式中不存在,additionalPropertiesfalse

这是非常误导的,有人可以指导我正确的方向吗?

JSON 架构定义是否additionalProperties仅适用于顶级字段而不适用于任何嵌套级别字段?

cus*_*der 5

extraProperties 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"
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

{
  a: {
    b: {
      c: {
        d: 42
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

一种有效的模式是:

{
  "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
                }
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

上面的模式非常冗长,但这里只是为了说明目的。$ref通过使用模式并将模式组合在一起,您应该能够使其更加简洁。