JSON Schema - Recursive Schema Definition

William picture William · Dec 24, 2013 · Viewed 17.2k times · Source

I have a JSON Schema

{
    'description': 'TPNode',
    'type': 'object',
    'id': 'tp_node',
    'properties': {
        'selector': {
            'type': 'string',
            'required': true
        }, 
        'attributes': {
            'type': 'array',
            'items': {
                'name': 'string',
                'value': 'string'
            }
        },
        'children': {
            'type': 'array',
            'items': {
                'type': 'object',
                '$ref': '#'
            }
        },
        'events': {
            'type': 'array',
            'items': { 
                'type': 'object',
                'properties': {
                    'type': {
                        'type': 'string'
                    },
                    'handler': {
                        'type': 'object'
                    },
                    'dependencies': {
                        'type': 'array',
                        'items': {
                            'type': 'string'
                        }
                     }
                 }
            }
        }
    }
}

What I'm trying to express in the children property is that it's an array of objects with the same exact schema. Is this the correct way to describe it?

Answer

cloudfeet picture cloudfeet · Dec 24, 2013

Yes, your schema will work. The "$ref": "#" points back to the root of the schema document.

However, the "type": "object" is useless:

{
    'type': 'object',
    '$ref': '#'
}

If $ref is present, then all other keywords are ignored. It would be better to remove type from the #/properties/children/items schema.