【问题标题】:Json schema permits override of fields in objects in arrayJson 模式允许覆盖数组中对象中的字段
【发布时间】:2020-03-09 08:54:15
【问题描述】:

我有一个像这样的 json

  "List": {
     {"Color": "red"},
     {}
  },
  "Color": "grey"
}

而这意味着默认颜色为灰色,并且列表中的对象可以覆盖此颜色。 只要存在默认颜色(同一级别列表中的属性),架构就应该允许 json 传递。如果没有,它只允许 json 通过模式检查,如果列表中的所有项目都指定了“颜色”。

我可以知道如何编写一个进行此检查的 json 模式吗?我知道 anyOf 但我认为它不能检查数组中的所有项目。

我试过了

{
  "type": "object",
  "properties": {
    "List": {"type": "array", "items": {"$ref:" "#/definitions/Item"}},
    "Color": {"type": "string"}
  },
  "definitions": {
    "Item": {"type: "object", "properties": " {"Color": {"type": "string"}}}
  },
  "anyOf": {
  {
    "type": "object",
      "required": ["Color"]  
  },
  {
    "type": "object",
    "List": {
      "type": "array",
      "items": {"$ref": "#/definitions/Item", "required": ["Color"]}
    }
  }
}

但验证器似乎没有选择 anyOf[1] 所需的颜色。 请帮忙。!谢谢。

【问题讨论】:

  • 您可以支付您尝试过的费用以便有人可以指导吗?

标签: json jsonschema


【解决方案1】:

另一个答案中的架构是正确的,但不必要地复杂。这是一个消除重复并使架构更易于阅读的示例。

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "List": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "Color": { "type": "string" },
          "Shape": { "type": "string" }
        }
      }  
    },
    "Color": { "type": "string" },
    "Shape": { "type": "string" }
  },
  "required": ["List"],
  "allOf": [
    { "$ref": "#/definitions/color-required-if-no-default-color" },
    { "$ref": "#/definitions/shape-required-if-no-default-shape" }
  ],
  "definitions": {
    "color-required-if-no-default-color": {
      "anyOf": [
        { "required": ["Color"] },
        {
          "properties": {
            "List": {
              "items": { "required": ["Color"] }
            }
          }
        }
      ]
    },
    "shape-required-if-no-default-shape": {
      "anyOf": [
        { "required": ["Shape"] },
        {
          "properties": {
            "List": {
              "items": { "required": ["Shape"] }
            }
          }
        }
      ]
    }
  }
}

【讨论】:

  • 这根本无法扩展。如果你有 10 个属性怎么办。
  • 我不明白你的意思。这显示了两个属性,您可以像前两个一样添加更多属性。而且,它以一种易于理解的方式组织事物,即使架构不断增长。
【解决方案2】:

我想我找到了办法:

架构如下所示

{

    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {"Color": {"type":"string"}, "Shape": {"type": "string"}},
    "allOf":[
      {
      "anyOf": [
      {
      "required": ["Color"],
      "properties": {"List": {"type": "array", "items": {"$ref": "#/definitions/Item"}}}
      },
      {
       "properties": {"List": {"type": "array", "items": {"$ref": "#/definitions/ColorItem"}}}
      }
      ]
      },
      {
      "anyOf": [
      {
      "required": ["Shape"],
      "properties": {"List": {"type": "array", "items": {"$ref": "#/definitions/Item"}}}
      },
      {
       "properties": {"List": {"type": "array", "items": {"$ref": "#/definitions/ShapeItem"}}}
      }
      ]
      }
    ],
   "required": ["List"],
   "definitions": {
   "Item": {
       "type": "object",
        "properties": {}
      },
   "ColorItem": {
       "allOf": [{"$ref": "#/definitions/Item"},
                 {"properties": {"Color": {"type": "string"}}, "required": ["Color"]}] 
   },
     "ShapeItem": {
       "allOf": [{"$ref": "#/definitions/Item"},
                 {"properties": {"Shape": {"type": "string"}}, "required": ["Shape"]}] 
   }
  }
}

基本上这就是我想要的。因此,只有从顶级 json 读取颜色/形状,或者我们可以在数组中找到它,它才会传递 json。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 2021-06-04
    • 2017-05-06
    • 1970-01-01
    相关资源
    最近更新 更多