为什么 json 模式验证缺少属性的 json

Why does json schema validate json with missing properties

我正在使用这个 json 模式验证器:https://www.jsonschemavalidator.net/ 来验证一些 json。令我惊讶的是,即使 json.

中缺少 属性,它也会验证模式

架构

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "properties": {
      "test": {
        "type": "array"
      }
    }
}

应该验证

{
    "test": []
}

不应该验证(但验证!)

{}

为什么这被认为是有效的,我如何验证 json 以便 属性 test 必须是 json 的一部分?

有一个required属性可以做到。默认情况下,验证似乎对所有属性使用 required: false

这验证并强制 属性 存在:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "properties": {
      "test": {
        "type": "array"
      }
    },
    "required": ["test"]
}