JSON 不同类型数组的架构

JSON schema for array of different types

我有非常基本的模式,但有些奇怪。

{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": 
{
    "$out": 
    {
        "type": "array",
        "minItems": 1,
        "items": {
            "oneOf": [
                { "type": "string" },
                { "$ref": "#/definitions/alias" }
            ]
        }
    }
},
"definitions": 
{
    "alias": 
    {
        "properties": 
        {
            "$source": { "type": "string" },
            "$alias": { "type": "string" }
        },
        "required": [ "$source", "$alias" ],
        "additionalProperties": false
    }
}

}

如果我用下面的JSON来测试:

{
    "$out": [
        "12w",
        { "$source": "WH.Code", "$alias": "WarehouseCode"}
    ] 
}

它失败了(sample)说数组中的字符串元素再次有效超过一个模式。如果我用 { "type": "string" } 更改对 'alias' 的引用,它会按预期工作。我做错了什么?

提前致谢。

您使用的所有关键字(propertiesrequiredadditionalProperties)仅在值是对象时适用。因为没有任何东西要求值是一个对象,所以任何不是对象的东西都会通过。仅当对象是对象时才会考虑对象关键字。

有多种方法可以使架构正常工作,但最直接的方法是将 "type": "object" 添加到 alias 架构。