JSON模式 - 递归模式定义

Wil*_*iam 17 json jsonschema

我有一个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'
                        }
                     }
                 }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

clo*_*eet 19

是的,您的架构将起作用.该"$ref": "#"点回模式文档的根.

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

{
    'type': 'object',
    '$ref': '#'
}
Run Code Online (Sandbox Code Playgroud)

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


Ser*_*min 17

使用id您需要引用的架构

'$ref': 'tp_node'
Run Code Online (Sandbox Code Playgroud)

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

  • 问题中提出的解决方案(使用"$ ref":"#"`)实际上更好,因为即使移动/重命名了架构它也能正常工作.使用您的解决方案,如果您重命名架构,那么您需要更改一堆*可能*是内部的引用. (3认同)
  • 这种解决方案是可取的,应该是公认的解决方案; 在模式发生变化的情况下,它会干净利落地显而易见,与使用"$ ref"的更普遍的相反:"#"`哪些更改可能会更难以诊断错误. (2认同)

小智 9

递归示例。

{
  "$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" }
  }
}
Run Code Online (Sandbox Code Playgroud)


cui*_*ing 7

使用定义和$ ref.

您可以将以下架构复制并粘贴到此在线json/schema编辑器并检查结果.

编辑截图:

编辑截图

架构代码:

{
    "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"
}
Run Code Online (Sandbox Code Playgroud)