Amid
Amid

Reputation: 22382

JSON schema for array of different types

I have very basic schema that behaves somehow strange.

{
"$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
    }
}

}

If I use the following JSON to test:

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

It fails (sample) saying that string element in array is valid agains more that one schema. If I change reference to 'alias' with say just { "type": "string" } it works as expected. What am I doing wrong?

Thanks in advance.

Upvotes: 0

Views: 131

Answers (1)

Jason Desrosiers
Jason Desrosiers

Reputation: 24489

All of the keywords you use (properties, required, additionalProperties) only apply if the value is an object. Because there nothing requiring that the value be an object, anything that is not an object will pass. The object keywords are only taken into account if it is an object.

There are a number of ways you can make the schema work, but the most straight forward is to add "type": "object" to your alias schema.

Upvotes: 1

Related Questions