Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Schema - Recursive Schema Definition

Tags:

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?

like image 990
William Avatar asked Dec 23 '13 23:12

William


People also ask

What is JSON Schema definition?

JSON Schema is a JSON media type for defining the structure of JSON data. JSON Schema provides a contract for what JSON data is required for a given application and how to interact with it. JSON Schema is intended to define validation, documentation, hyperlink navigation, and interaction control of JSON data.

What is a JSON Schema used for?

JSON Schema is a lightweight data interchange format that generates clear, easy-to-understand documentation, making validation and testing easier. JSON Schema is used to describe the structure and validation constraints of JSON documents.

What is JSON Schema additionalProperties?

The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.

What does $Ref mean in JSON Schema?

In a JSON schema, a $ref keyword is a JSON Pointer to a schema, or a type or property in a schema. A JSON pointer takes the form of A # B in which: A is the relative path from the current schema to a target schema. If A is empty, the reference is to a type or property in the same schema, an in-schema reference.


2 Answers

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.

like image 150
cloudfeet Avatar answered Oct 03 '22 18:10

cloudfeet


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

like image 43
Sergey Eremin Avatar answered Oct 03 '22 18:10

Sergey Eremin