title: parent/child hierarchy & jsonschema
body: <p>I'm trying to come up with a json schema to validate this kind of document:</p>
  
  <pre><code> {
    "rootData": {
      "parent1": [
        {
          "parent1": [
            {
              "leaf1": {
                "attr": "value"
              }
            }
          ]
        },
        {
          "parent2": [
            {
              "leaf1": {
                "attr": "value"
              }
            }
          ]
        },
        {
          "leaf1": {
            "attr": "value"
          }
        }
      ]
    },
    "rootInfo": {
      "i1": 1
    }
   }
  </code></pre>
  
  <p>Some information:</p>
  
  <ol>
  <li>"rootData" can contain only one parent, either parent1 or parent2</li>
  <li>parent1/parent2 can be switched for parent2/parent1 at any position in the hierarchy</li>
  <li>there is no depth limit: as long as a parent contains another parent, we can go deeper and deeper.</li>
  </ol>
  
  <p>I think I was able to figure out how to represent the hierarchical nature of the embedded parent/child part.
  But I don't know how to define the "rootData" top level, more precisely the choice between "parent1" or "parent2" since I can not use oneOf (I think...)</p>
  
  <pre><code>    {
    "$schema": "http://json-schema.org/draft-06/schema#",
    "definitions": {
      "parent1": {
        "$id": "#parent1",
        "type": "object",
        "properties": {
          "parent1": {
            "$ref": "#node"
          }
        }
      },
      "parent2": {
        "$id": "#parent2",
        "type": "object",
        "properties": {
          "parent2": {
            "$ref": "#node"
          }
        }
      },
      "leaf1": {
        "$id": "#leaf1",
        "type": "object",
        "properties": {
          "leaf1": {
            "type": "object",
            "properties": {
              "attr": {
                "type": "string"
              }
            },
            "required": [
              "attr"
            ]
          }
        }
      },
      "node": {
        "$id": "#node",
        "type": "array",
        "items": {
          "oneOf": [
            {
              "$ref": "#parent1"
            },
            {
              "$ref": "#parent2"
            },
            {
              "$ref": "#leaf1"
            }
          ]
        }
      }
    },
    "type": "object",
    "properties": {
      "rootData": {
        "properties": {
          //I tried several things here minProperties/maxProperties or oneOf ... but I actually don't know how to reuse my previous parent1 or parent2 definitions ...
          }
      }
    }
  }
  </code></pre>
  
  <p>Any help is welcome!</p>
  
  <p>Thx</p>