简体   繁体   English

父/子层次结构和 jsonschema

[英]parent/child hierarchy & jsonschema

I'm trying to come up with a json schema to validate this kind of document:我正在尝试提出一个 json 模式来验证这种文档:

 {
  "rootData": {
    "parent1": [
      {
        "parent1": [
          {
            "leaf1": {
              "attr": "value"
            }
          }
        ]
      },
      {
        "parent2": [
          {
            "leaf1": {
              "attr": "value"
            }
          }
        ]
      },
      {
        "leaf1": {
          "attr": "value"
        }
      }
    ]
  },
  "rootInfo": {
    "i1": 1
  }
 }

Some information:一些信息:

  1. "rootData" can contain only one parent, either parent1 or parent2 “rootData”只能包含一个父级,parent1 或 parent2
  2. parent1/parent2 can be switched for parent2/parent1 at any position in the hierarchy parent1/parent2 可以在层次结构中的任何位置切换为 parent2/parent1
  3. there is no depth limit: as long as a parent contains another parent, we can go deeper and deeper.没有深度限制:只要一个父级包含另一个父级,我们就可以越来越深入。

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...)但我不知道如何定义“rootData”顶层,更准确地说是“parent1”或“parent2”之间的选择,因为我不能使用 oneOf(我认为......)

    {
  "$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 ...
        }
    }
  }
}

Any help is welcome!欢迎任何帮助!

Thx谢谢

Add "required":["parent1"] and "required":["parent2"] constraints to your parent1 and parent2 definitions, and use oneOf :"required":["parent1"]"required":["parent2"]约束添加到您的 parent1 和 parent2 定义,并使用oneOf

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "definitions": {
        "parent1": {
            "$id": "#parent1",
            "type": "object",
            "required": ["parent1"],
            "properties": {
                "parent1": {"$ref": "#node"}
            }
        },
        // ...
    },
    "type": "object",
    "properties": {
        "rootData": {
            "oneOf": [
                {"$ref": "#parent1"},
                {"$ref": "#parent2"}
            ]
        }
    }
}

If you tried using anyOf and that worked, that's because your parent1 and parent2 definitions validate against each other: parent1 validates against a parent2 object, since it ignores the "parent2" property, and is valid without a "parent1" property;如果您尝试使用anyOf并且有效,那是因为您的parent1parent2定义相互验证: parent1针对parent2对象验证,因为它忽略“parent2”属性,并且在没有“parent1”属性的情况下有效; and vice-versa for "parent1".反之亦然“parent1”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM