【问题标题】:How to define the min size of array in the json schema如何在 json 模式中定义数组的最小大小
【发布时间】:2013-05-11 02:43:01
【问题描述】:

我想制作一个 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"]
}
}

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

我需要添加最小定义吗?

【问题讨论】:

    标签: arrays json jsonschema


    【解决方案1】:

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

    见:

    https://datatracker.ietf.org/doc/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"]
      }
    

    【讨论】:

    • 一个例子比链接更有用(随着时间的推移可能会腐烂)
    • 它按预期工作:当"maxItems": 0 时,如果"items" 数组的长度为非零,则验证失败。太好了!
    • 我怎样才能为每个项目添加 minLength / max Length (String) { "$schema": "json-schema.org/draft-04/schema#", "title": "Product", "description": "A product from Acme 的目录”, “type”: “object”, “properties”: { ... “tags”: { “type”: “array”, “items”: { “type”: “string”, “minLength”: 1, "maxLength": 25 }, "minItems": 1, "maxItems": 4, "uniqueItems": true } }, "required": ["id", "name", "price"] }
    • 您可以为字符串添加一个定义,看起来像这样(这个有大小模式):“definitions”:{“Id”:{“description”:“CktID描述,可以如果用作列表末尾,则大小为 0", "type":"string", "pattern" : "^([\u0020-\u007f]){0,28}$" },
    【解决方案2】:

    我想没有,至少在寻找工作草案时,minimum 仅适用于数值,而不适用于数组。

    5.1。数字实例(数字和整数)的验证关键字
    ...
    5.1.3. minimum and exclusiveMinimum

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

    【讨论】:

      【解决方案3】:

      看起来 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)。

      【讨论】:

        猜你喜欢
        • 2022-01-22
        • 2021-06-23
        • 2018-01-03
        • 2020-04-15
        • 2013-01-19
        • 1970-01-01
        • 2021-06-17
        • 2021-01-11
        • 1970-01-01
        相关资源
        最近更新 更多