【问题标题】:JSON Schema - Recursive Schema DefinitionJSON 模式 - 递归模式定义
【发布时间】:2014-01-12 05:07:39
【问题描述】:

我有一个 JSON 架构

{
    'description': 'TPNode',
    'type': 'object',
    'id': 'tp_node',
    'properties': {
        'selector': {
            'type': 'string',
            'required': true
        }, 
        'attributes': {
            'type': 'array',
            'items': {
                'name': 'string',
                'value': 'string'
            }
        },
        'children': {
            'type': 'array',
            'items': {
                'type': 'object',
                '$ref': '#'
            }
        },
        'events': {
            'type': 'array',
            'items': { 
                'type': 'object',
                'properties': {
                    'type': {
                        'type': 'string'
                    },
                    'handler': {
                        'type': 'object'
                    },
                    'dependencies': {
                        'type': 'array',
                        'items': {
                            'type': 'string'
                        }
                     }
                 }
            }
        }
    }
}

我想在 children 属性中表达的是,它是一个具有相同模式的对象数组。这是描述它的正确方式吗?

【问题讨论】:

  • 为什么使用 v3 语法? "required" 是 v4 中的一个数组。
  • 你是对的。但是,我通过 JSON.NET 验证架构,我发现它不支持 v4 语法。

标签: json jsonschema


【解决方案1】:

使用您需要引用的架构的id

'$ref': 'tp_node'

请看这里: http://json-schema.org/latest/json-schema-core.html#anchor30

【讨论】:

  • 问题中提出的解决方案(使用"$ref": "#")实际上更好,因为即使架构被移动/重命名它也能工作。使用您的解决方案,如果您重命名架构,那么您需要更改一堆 可能 是内部的引用。
  • 此解决方案更可取,应该是公认的解决方案;如果架构发生更改,它会干净而明显地中断,而不是使用"$ref": "#" 的更通用的架构,更改可能会产生更难诊断的错误。
  • 迟到了,指向自身会导致循环引用错误,我在某处读到不建议这样做。但很有趣,我发现了这个github。我希望我也可以这样做,也许这是我使用的架构版本(draft-6)。
【解决方案2】:

是的,您的架构将起作用。 "$ref": "#" 指向架构文档的根。

但是,"type": "object" 没用:

{
    'type': 'object',
    '$ref': '#'
}

如果存在$ref,则忽略所有其他关键字。最好从 #/properties/children/items 架构中删除 type

【讨论】:

    【解决方案3】:

    使用定义和 $ref。

    您可以将以下架构复制并粘贴到此online json/schema editor 并检查结果。

    编辑器截图:

    架构代码:

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

    【讨论】:

      【解决方案4】:

      递归示例。

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

      【讨论】:

        猜你喜欢
        • 2014-09-19
        • 1970-01-01
        • 2021-10-30
        • 2017-05-21
        • 1970-01-01
        • 1970-01-01
        • 2014-08-30
        • 2018-02-05
        • 2015-08-23
        相关资源
        最近更新 更多