【问题标题】:What if `items` and `type` were defined as arrays, without anyOf property如果 `items` 和 `type` 被定义为数组,没有 anyOf 属性怎么办
【发布时间】:2017-08-08 17:58:10
【问题描述】:

core/validation meta-schemaitemstype属性中定义如下:

项目

"items": {
    "anyOf": [
        { "$ref": "#" },
        {
            "type": "array",
            "minItems": 1,
            "items": { "$ref": "#" }
        }
    ],
    "default": {}
},

类型

"type": {
    "anyOf": [
        { "$ref": "#/definitions/simpleTypes" },
        {
            "type": "array",
            "items": { "$ref": "#/definitions/simpleTypes" },
            "minItems": 1,
            "uniqueItems": true
        }
    ]
},

据我了解:

  • items 可以是一个模式或至少一个模式的数组
  • type 可以是 simpleType 或至少一个 simpleType 的数组

除了写作的实用性和舒适性之外,我是否认为这些定义是等价的?

项目

"items": {
    "type": "array",
    "items": { "$ref": "#" }
    "default": [{}]
},

类型

"type": {
    "type": "array",
    "items": { "$ref": "#/definitions/simpleTypes" },
    "uniqueItems": true
},

换句话说,以下两种模式的解释是否存在差异:

架构 #1

{
    "type": "array",
    "items": {
        "type": "string"
    }
}

模式 #2

{
    "type": ["array"],
    "items": [
        {
            "type": "string"
        }
    ]
}

【问题讨论】:

    标签: jsonschema


    【解决方案1】:

    回答“换句话说”部分:

    • "type": "array""type": ["array"] 之间没有语义差异

    • 但“项目”不同:

      • "items": {"type": "string"} 表示包含任意数量字符串项的数组
      • "items": [{"type": "string"}] 表示任意数量元素的数组,其中第 0 个必须是字符串,其余的可以是任何元素

    您可以在此处找到有关列表与元组验证的更多信息:https://spacetelescope.github.io/understanding-json-schema/reference/array.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-05
      • 2021-11-26
      • 2019-12-24
      • 1970-01-01
      相关资源
      最近更新 更多