William William - 28 days ago 15
JSON Question

JSON Schema - Recursive Schema Definition

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

Use the id of the schema you need to reference

'$ref': 'tp_node'

See here: http://json-schema.org/latest/json-schema-core.html#anchor30