如何使用 JSON 架构按类型限制数组中的最大项目数?
How do I limit the maximum number of items in array by type using JSON Schema?
我有一个数组类型的 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_images
、description_box
和 button
中任意数量的组件。
我想根据组件类型限制每个组件
- banner_images -> 1
- description_box -> 5
- 按钮 -> 10
有一个选项可以设置数组类型的项目的长度https://json-schema.org/understanding-json-schema/reference/array.html#id7
但是如何根据类型限制项目的长度?
您可以通过组合“contains”和“maxContains”来做到这一点(请注意,这需要支持 2019-09 或更高版本草案的实现。)
在伪代码中:components 数组的项目包含具有 属性“类型”的对象。当“type”为“banner_images”时,最多可存在1个这样的项目。当“type”为“description_box”时,最多可存在5个这样的条目。当“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
},
]
我有一个数组类型的 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_images
、description_box
和 button
中任意数量的组件。
我想根据组件类型限制每个组件
- banner_images -> 1
- description_box -> 5
- 按钮 -> 10
有一个选项可以设置数组类型的项目的长度https://json-schema.org/understanding-json-schema/reference/array.html#id7
但是如何根据类型限制项目的长度?
您可以通过组合“contains”和“maxContains”来做到这一点(请注意,这需要支持 2019-09 或更高版本草案的实现。)
在伪代码中:components 数组的项目包含具有 属性“类型”的对象。当“type”为“banner_images”时,最多可存在1个这样的项目。当“type”为“description_box”时,最多可存在5个这样的条目。当“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
},
]