【问题标题】:JSON Schema: array with exactly n elements of given sub-schemasJSON Schema:具有给定子模式的恰好 n 个元素的数组
【发布时间】:2015-06-23 15:49:29
【问题描述】:

我试图弄清楚如何为必须包含恰好 2 个元素的数组编写 JSON 模式,其中每个元素都符合其自己的子模式。我完全不知道,因为anyOfallOfoneOf 都不适合这里。

假设我有 ss1ss2 子模式,它们分别定义了 t1t2 类型的元素。如何编写一个模式来接受数组,其中一个元素类型为t1(符合ss1)和另一个类型元素t2(符合ss2)?

【问题讨论】:

    标签: arrays json jsonschema


    【解决方案1】:

    items 关键字为此具有特殊格式。它可以是一个模式数组,而不是一个模式。当items以这种方式使用时,数组中的项必须符合items模式数组中对应的模式。这是一个假设 t1 = string 和 t2 = integer 的工作示例。

    {
      "type": "array",
      "items": [
        { "$ref": "#/definitions/ss1" },
        { "$ref": "#/definitions/ss2" }
      ],
      "minItems": 2,
      "maxItems": 2,
      "definitions": {
        "ss1": { "type": "string" },
        "ss2": { "type": "integer" }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      相关资源
      最近更新 更多