【发布时间】: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_images、description_box和button中任意数量的组件。
我想根据组件类型限制每个组件
- banner_images -> 1
- description_box -> 5
- 按钮 -> 10
有一个选项可以设置数组类型https://json-schema.org/understanding-json-schema/reference/array.html#id7中项目的长度
但是如何根据类型限制项目的长度?
【问题讨论】:
标签: jsonschema