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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16583485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:46:53  来源:igfitidea点击:

How to define the min size of array in the json schema

arraysjsonjsonschema

提问by Stella

I want to make a schema of json file.It's for an array of products.

我想制作一个 json 文件的模式。它用于一系列产品。

The json schema is similar as below:

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"]
}
}

The array should at least one item in it. How can I define the minimum of the array?

数组中应该至少有一项。如何定义数组的最小值?

Do I need to add the minimun defination?

我需要添加最小定义吗?

采纳答案by dmi3y

I suppose no, at least looking to working draft the minimumis applied only for numeric values, not arrays.

我想不会,至少在工作草案中minimum只适用于数值,而不是数组。

5.1. Validation keywords for numeric instances (number and integer)
...
5.1.3. minimum and exclusiveMinimum

5.1. 数字实例的验证关键字(数字和整数)
...
5.1.3。最小值和唯一性最小值

So you should be good with min/maxItems for arrays.

所以你应该擅长数组的 min/maxItems。

回答by damorin

To set the minimum # of item in an array, use the "minItems".

要设置数组中项目的最小数量,请使用"minItems".

See:

看:

http://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.3

http://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.3

and

http://jsonary.com/documentation/json-schema/?section=keywords/Array%20validation

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"]
  }

回答by U007D

It looks like draft v4 permits what you are looking for. From http://json-schema.org/example1.html:

看起来草案 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"]
}

Notice that the "tags" property is defined as an array, with a minimum number of items (1).

请注意,“tags”属性被定义为一个数组,其中包含最少数量的项目 (1)。