【问题标题】:How to set JSON validation schema for general properties values如何为常规属性值设置 JSON 验证模式
【发布时间】:2019-01-28 21:31:32
【问题描述】:

我想使用模式验证 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"}
    }
  }
}

谢谢。

【问题讨论】:

    标签: json jsonschema


    【解决方案1】:

    additionalProperties 正是为此目的:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-29
      • 1970-01-01
      • 2019-09-14
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多