如果 `items` 和 `type` 被定义为数组,没有 anyOf 属性

What if `items` and `type` were defined as arrays, without anyOf property

core/validation meta-schema中,itemstype属性定义如下:

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

回答 "in other words" 部分:

  • "type": "array""type": ["array"]

  • 之间没有语义区别
  • 但 "items" 不同:

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

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