Ste*_*lla 45 arrays json jsonschema
我想制作一个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"]
}
}
Run Code Online (Sandbox Code Playgroud)
该数组至少应包含一个项目.如何定义数组的最小值?
我是否需要添加最小化定义?
小智 63
要设置数组中项目的最小数量,请使用"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"]
}
Run Code Online (Sandbox Code Playgroud)
我想不,至少在寻找工作草案时,minimum它仅适用于数值,而不适用于数组.
5.1.数字实例的验证关键字(数字和整数)
...
5.1.3.最小和独占最小值
所以你应该对数组的min/maxItems很好.
看起来草案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"]
}
Run Code Online (Sandbox Code Playgroud)
请注意,"tags"属性被定义为一个数组,具有最小数量的项(1).