How to use definitions in JSON schema (draft-04)

Solution 1:

I think the recommended approach is the one shown in Json-Schema web, Example2. You need to use an enum to select schemas "by value". In your case it would be something like:

{
    "type": "object",
    "required": [ "results" ],
    "properties": {
        "results": {
            "type": "array",
            "items": {
                "oneOf": [
                    { "$ref": "#/definitions/person" },
                    { "$ref": "#/definitions/company" }
                ]
            }
        }
    },
    "definitions": {
        "person": {
            "properties": {
                "type": { "enum": [ "person" ] },
                "name": {"type": "string" },
                "dateOfBirth": {"type":"string"}
            },
            "required": [ "type", "name", "dateOfBirth" ],
            "additionalProperties": false
        },
        "company": {
            "properties": {
                "type": { "enum": [ "company" ] },
                . . . 
            }        
        }
    }
}

Solution 2:

Sorry,

I don't get the point. The question is about the 'dependencies' keyword which is part of the last JSON Schema specification, right?

I do not find 'dependencies' in the accepted answer (?)

It is briefly explained in the last draft. But http://usingjsonschema.com explained both property and definition dependencies in the book:

http://usingjsonschema.com/assets/UsingJsonSchema_20140814.pdf

start at page 29 (see, explained at page 30)

"dependencies": {
     "shipTo":["shipAddress"],
     "loyaltyId":["loyaltyBonus"]
}