JSON Schema - Recursive Schema Definition

前端 未结 4 329
情话喂你
情话喂你 2020-12-10 02:15

I have a JSON Schema

{
    \'description\': \'TPNode\',
    \'type\': \'object\',
    \'id\': \'tp_node\',
    \'properties\': {
        \'selector\': {
             


        
相关标签:
4条回答
  • 2020-12-10 02:30

    Recursion example.

    {
      "$schema": "http://json-schema.org/draft-07/schema#",
    
      "definitions": {
        "person": {
          "type": "object",
          "properties": {
            "name": { "type": "string" },
            "children": {
              "type": "array",
              "items": { "$ref": "#/definitions/person" },
              "default": []
            }
          }
        }
      },
    
      "type": "object",
    
      "properties": {
        "person": { "$ref": "#/definitions/person" }
      }
    }
    
    0 讨论(0)
  • 2020-12-10 02:33

    use definitions and $ref.

    You can copy and paste the following schema to this online json/schema editor and check the results.

    editor screenshot:

    schema code:

    {
        "definitions": {
            "TPNode": {
                "title": "TPNode",
                "description": "TPNode",
                "type": "object",
                "properties": {
                    "selector": {
                        "type": "string",
                        "required": true
                    }, 
                    "attributes": {
                        "type": "array",
                        "items": {
                            "title": "Attribute",
                            "type": "object",
                            "properties": {
                                "name": {
                                    "type": "string"
                                },
                                "value": {
                                    "type": "string"
                                }
                            }
                        }
                    },
                    "children": {
                        "type": "array",
                        "items": {
                            "$ref": "#/definitions/TPNode"
                        }
                    },
                    "events": {
                        "type": "array",
                        "items": { 
                            "title": "Event",
                            "type": "object",
                            "properties": {
                                "type": {
                                    "type": "string"
                                },
                                "handler": {
                                    "type": "object"
                                },
                                "dependencies": {
                                    "type": "array",
                                    "items": {
                                        "type": "string"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        "$ref": "#/definitions/TPNode"
    }
    
    0 讨论(0)
  • 2020-12-10 02:41

    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

    0 讨论(0)
  • 2020-12-10 02:48

    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.

    0 讨论(0)
提交回复
热议问题