【问题标题】:How do I limit the maximum number of items in array by type using JSON Schema?如何使用 JSON Schema 按类型限制数组中的最大项目数?
【发布时间】:2021-04-18 15:20:58
【问题描述】:

我有一个数组类型的 json-schema (draft-07) 来存储多种类型的数据,例如

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "required": [
      "type",
      "data"
    ],
    "additionalProperties": false,
    "properties": {
      "type": {
        "type": "string",
        "enum": [
          "banner_images",
          "description_box",
          "button"
        ]
      },
      "data": {
        "type": "object"
      }
    },
    "allOf": [
      {
        "if": {
          "properties": {
            "type": {
              "const": "banner_images"
            }
          }
        },
        "then": {
          "properties": {
            "data": {
              "$ref": "components/banner_images.json"
            }
          }
        }
      },
      {
        "if": {
          "properties": {
            "type": {
              "const": "description_box"
            }
          }
        },
        "then": {
          "properties": {
            "data": {
              "$ref": "components/description_box.json"
            }
          }
        }
      },
      {
        "if": {
          "properties": {
            "type": {
              "const": "button"
            }
          }
        },
        "then": {
          "properties": {
            "data": {
              "$ref": "components/button.json"
            }
          }
        }
      }
    ]
  }
}

验证以下数据

[
  {
    "type": "banner_images",
    "data": {
      "images": [
        {
           "url": "https://example.com/image.jpg"
        }
      ]
    }
  },
  {
    "type": "description_box",
    "data": {
      "text": "Description box text"
    }
  },
  {
    "type": "button",
    "data": {
      "label": "Click here",
      "color": {
        "label": "#ff000ff",
        "button": "#00ff00",
        "border": "#00ff00"
      },
      "link": "https://example.com"
    }
  }
]

截至目前,用户可以提供banner_imagesdescription_boxbutton中任意数量的组件。

我想根据组件类型限制每个组件

  • banner_images -> 1
  • description_box -> 5
  • 按钮 -> 10

有一个选项可以设置数组类型https://json-schema.org/understanding-json-schema/reference/array.html#id7中项目的长度

但是如何根据类型限制项目的长度?

【问题讨论】:

    标签: jsonschema


    【解决方案1】:

    您可以通过组合“contains”和“maxContains”来做到这一点(请注意,这需要支持草稿版本 2019-09 或更高版本的实现。)

    在伪代码中:组件数组中的项目包含具有“类型”属性的对象。当“type”为“banner_images”时,最多可以存在1个这样的项目。当“type”为“description_box”时,最多可以存在5个这样的item。当“type”为“button”时,最多可以有10个这样的item。

    即:

      "items": {
        "type": "object",
        ...
      },
      "allOf": [
        {
           "contains": {
             "properties": {
                "type": {
                  "const": "banner_images"
                }
             }
           },
           "minContains": 0,
           "maxContains": 1
        },
        {
           "contains": {
             "properties": {
                "type": {
                  "const": "description_box"
                }
             }
           },
           "minContains": 0,
           "maxContains": 5
        },
        {
           "contains": {
             "properties": {
                "type": {
                  "const": "button"
                }
             }
           },
           "minContains": 0,
           "maxContains": 10
        },
      ]
    
    
    

    【讨论】:

    • 以太,如果你能提供一个例子就好了。
    猜你喜欢
    • 2021-03-27
    • 2013-03-02
    • 2016-07-21
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多