递归自引用JSON-Schema

时间:2016-02-07 06:48:40

标签: jsonschema

它是一个有效的json架构:

  object:
    $ref: '#/definitions/object'

你会建议使用这种格式吗?

1 个答案:

答案 0 :(得分:8)

自我引用是允许且有用的。但是,您的示例看起来只是一个参考无限循环。下面是一个JSON模式的示例,它使用递归引用来定义无限深度的树结构。

{
  "type": "object",
  "properties": {
    "name": { "type": "string" },
    "tree": { "$ref": "#/definitions/tree" }
  },
  "definitions": {
    "tree": {
      "type": "object",
      "properties": {
        "value": { "type": "string" },
        "branches": {
          "type": "array",
          "items": { "$ref": "#/definitions/tree" },
          "minItems": 1
        }
      },
      "required": ["value"]
    }
  }
}
相关问题