【问题标题】:JSON Schema for child objects with different set of keys具有不同键集的子对象的 JSON 模式
【发布时间】:2019-12-18 10:20:06
【问题描述】:

我有 JSON 数据,它是一个数据数组,例如

[
  {
    "type": "background_color",
    "data": {
      "backgroundColor": "F9192D"
    }
  },
  {
    "type": "banner_images",
    "data": {
      "images": [
        {
          "url": "https://example.com/abc.jpg",
          "id": 3085
        },
        {
          "url": "https://example.com/zyx.jpg",
          "id": 3086
        }
      ]
    }
  },
  {
    "type": "description_box",
    "data": {
      "text": "Hello 56787"
    }
  }
]

数据是一个对象数组,它有两个键 typedatadata 的类型和键将由它拥有的数据的type 定义。

background_color 类型一样,data 应该有backgroundColor 属性,而对于banner_imagesdata 应该有images,这是一个其他属性的数组。

到目前为止,我所做的是

{
  "definitions": {},
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "title": "category schema",
  "description": "Used to validate data of category",
  "examples": [],
  "required": [],
  "items": {
    "type": "object",
    "required": [
      "type",
      "data"
    ],
    "properties": {
      "type": {
        "type": "string",
        "enum": ["background_color", "banner_images", "description_box"]
      },
      "data": {
        "type": "object"        // How to define data property here for each use case
      }
    }
  }
}

我不知道如何为每个用例定义 data 属性?

【问题讨论】:

  • 你知道所有可能的类型吗?如果是这样,我可以为您提供答案=]
  • 是的,根据documentation,它们是nullstringnumberarraybooleanobjectinteger
  • 抱歉,我的意思是您想在数据中定义为可能类型的值,例如 banner_images
  • 是的,我知道所有可能的类型。

标签: json jsonschema json-schema-validator


【解决方案1】:

您可以使用if/then/else 块来定义条件约束。

ifthen 的值是模式。如果if 架构有效,则应用then 架构,否则,allOf 子架构(本例中为allOf[0])将通过验证。

有几种不同的方法可以做到这一点,但是当您没有任何额外或特殊要求时,这是干净的。如果你这样做,请回来=]

在这个例子中,我添加了banner_images...

您可以测试它是否工作here

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "title": "category schema",
  "description": "Used to validate data of category",
  "items": {
    "type": "object",
    "required": [
      "type",
      "data"
    ],
    "properties": {
      "type": {
        "type": "string",
        "enum": [
          "background_color",
          "banner_images",
          "description_box"
        ]
      },
      "data": {
        "type": "object"
      }
    },
    "allOf": [
      {
        "if": {
          "properties": {
            "type": {
              "const": "banner_images"
            }
          }
        },
        "then": {
          "properties": {
            "data": {
              "required": [
                "images"
              ],
              "properties": {
                "images": {
                  "type": "array"
                }
              }
            }
          }
        }
      }
    ]
  }
}

作为参考,以下是 JSON Schema Draft-7 规范文档中详细说明行为的部分:https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.6

【讨论】:

  • 感谢 Relequestual,是否可以验证数据中不允许的额外键。我的意思是将对象中的键限制为模式中列出的唯一键。
  • 欢迎。当然。在then 架构中,添加"additionalProperties": false。您可能还想添加 "else": false,但鉴于您已经要求 type 是枚举中的值之一,您不需要添加它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-14
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多