强制对象的属性为JSON模式中列出的属性

wtzytmuj  于 2022-11-26  发布在  其他
关注(0)|答案(2)|浏览(110)

如何强制“object”属性只允许列出的属性成为它的一部分,而不允许其他属性成为它的一部分,尤其是空键“"?

{
  "type": "object",
  "properties": {
    "object": {
      "type": "object",
      "properties": {
        "property1": {
          "type": "string"
        },
        "property2": {
          "type": "string"
        },
        "property3": {
          "type": "string"
        }
      },
      "uniqueItems": true
    }
  }
}
5jdjgkvh

5jdjgkvh1#

我相信,你要找的是"additionalProperties": false
下面是添加了additionalProperties的模式。请在此处尝试:https://www.jsonschemavalidator.net/s/RDGrQvL6
请注意,我必须从模式中删除额外的"object" Package 器才能使其正常工作。

{
  "type": "object",
  "properties": {
    "property1": {
      "type": "string"
    },
    "property2": {
      "type": "string"
    },
    "property3": {
      "type": "string"
    }
  },
  "additionalProperties": false
}
xwbd5t1u

xwbd5t1u2#

丹尼尔的答案是正确的,"additionalProperties": false也是我需要的,但下面的代码是我实际上想用嵌套对象实现的。

{
  "type": "object",
  "properties": {
    "object": {
      "type": "object",
      "properties": {
        "property1": {
          "type": "string"
        },
        "property2": {
          "type": "string"
        },
        "property3": {
          "type": "string"
        }
      },
      "uniqueItems": true,
      "additionalProperties": false // will not allow any other property other than 'property1', 'property2' or 'property3' as part of the nested object "object".
    }
  }
}

相关问题