Json 架构允许覆盖数组中对象中的字段

Json schema permits override of fields in objects in array

我有一个 json 看起来像这样

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

而这意味着默认颜色为灰色,列表中的对象可以覆盖此颜色。 只要存在默认颜色(同一级别列表中的 属性),架构就应该允许 json 通过。如果不是,它只允许 json 在列表中的所有项目都指定了 "Color".

时通过模式检查

我可以知道如何编写 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] 所需的颜色。 请帮忙。!谢谢。

我想我找到了一个方法:

架构如下

{

    "$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 读取 Color/Shape 或者我们可以在数组中找到它时,它才会通过 json。

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

{
  "$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"] }
            }
          }
        }
      ]
    }
  }
}