如何为常规属性值设置JSON验证模式

时间:2018-08-22 15:11:12

标签: json jsonschema

我想使用架构来验证JSON(当前是草案6,但如果需要,我们可以进行升级)。我的情况是一个对象,其属性值均具有相同的结构,例如:

{
  "blueFoo": {
    "bar1": "someValue",
    "bar2": "differentValue"
  },
  "redFoo": {
    "bar1": "someOtherValue",
    "bar2": "LoremYpsum"
  },
  "purpleFoo": {
    "bar1": "anotherString",
    "bar2": "nextValue"
  },
  ...
}

是否可以为常规属性值设置验证模式?像这样:

{
  "type": "object",
  "propertyValue": {
    "type": "object",
    "required": ["bar1", "bar2"],
    "additionalProperties": false,
    "properties": {
      "bar1": {"type": "string"},
      "bar2": {"type": "string"}
    }
  }
}

谢谢。

1 个答案:

答案 0 :(得分:1)

additionalProperties正是用于此目的:

{
 "type": "object",
 "additionalProperties": {
    "properties": {
      "bar1": {"type": "string"},
      "bar2": {"type": "string"}
    },
    "required": ["bar1", "bar2"],
    "additionalProperties": false
 }
}