Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在json模式中定义数组的最小大小_Arrays_Json_Jsonschema - Fatal编程技术网

Arrays 如何在json模式中定义数组的最小大小

Arrays 如何在json模式中定义数组的最小大小,arrays,json,jsonschema,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": { "descript

我想做一个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"]
}
}
数组中至少应包含一项。如何定义数组的最小值


我需要添加最小定义吗?

我想不需要,至少在工作草案中,
最小定义只适用于数值,而不是数组

5.1。数字实例的验证关键字(数字和整数)


因此,您应该对数组的min/maxItems很在行。

看起来v4草案允许您所需的内容。发件人:

请注意,“tags”属性被定义为一个数组,具有最少数量的项(1)。

要设置数组中最少数量的项,请使用
“minItems”

见:


一个例子比链接(可能会随着时间而腐烂)更有用。它按预期工作:当
“maxtems”:0
时,如果
“items”
数组的长度为非零,验证将失败。伟大的
{
"$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"]
}
   {
   "$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"]
  }