【问题标题】:Recursive Self Referencing JSON-Schema递归自引用 JSON 模式
【发布时间】:2016-02-07 06:48:39
【问题描述】:

它是一个有效的 json 架构吗:

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

你会推荐使用这种格式吗?

【问题讨论】:

  • 这个运气好吗?

标签: jsonschema


【解决方案1】:

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

{
  "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"]
    }
  }
}

【讨论】:

  • 是否可以为递归指定maxDepth?
  • 不。 JSON Schema 中无法知道您所处的深度,因此无法限制某个深度。
猜你喜欢
  • 2014-09-19
  • 2014-01-12
  • 2018-02-05
  • 2018-01-03
  • 2014-08-30
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 2017-07-27
相关资源
最近更新 更多