我想制作一个json文件架构。它适用于一系列产品。
json架构类似如下:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product set",
"type": "array",
"items": {
"title": "Product",
"type": "object",
"properties": {
"id": {
"description": "The unique identifier for a product",
"type": "number"
},
"name": {
"type": "string"
},
"price": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": true
},
"dimensions": {
"type": "object",
"properties": {
"length": {"type": "number"},
"width": {"type": "number"},
"height": {"type": "number"}
},
"required": ["length", "width", "height"]
},
"warehouseLocation": {
"description": "Coordinates of the warehouse with the product",
"$ref": "http://json-schema.org/geo"
}
},
"required": ["id", "name", "price"]
}
}
数组中至少应包含一个项目。如何定义数组的最小值?
我是否需要添加最小化定义?
答案 0 :(得分:60)
要设置数组中项目的最小数量,请使用“minItems”。
请参阅:
http://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.3
和
http://jsonary.com/documentation/json-schema/?section=keywords/Array%20validation
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
...
"tags": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"maxItems": 4,
"uniqueItems": true
}
},
"required": ["id", "name", "price"]
}
答案 1 :(得分:8)
我认为不,至少在寻找工作草案minimum
仅适用于数值而非数组。
5.1。数字实例的验证关键字(数字和整数)
...
5.1.3. minimum and exclusiveMinimum
所以你应该对数组的min / maxItems有好处。
答案 2 :(得分:5)
看起来草案v4允许您正在寻找的内容。来自http://json-schema.org/example1.html:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
...
"tags": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": true
}
},
"required": ["id", "name", "price"]
}
请注意,“tags”属性被定义为一个数组,具有最少的项目数(1)。
答案 3 :(得分:-2)
如果您期望一个空数组,请按此定义。
{
"items": [ {} ],
"additionalItems": false
}