JSON模式 - 递归模式定义

 PHP小龙 发布于 2023-02-08 13:35

我有一个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属性中表达的是它是一个具有相同精确模式的对象数组.这是描述它的正确方法吗?

3 个回答
  • 使用id您需要引用的架构

    '$ref': 'tp_node'
    

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

    2023-02-08 13:37 回答
  • 使用定义和$ 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"
    }
    

    2023-02-08 13:37 回答
  • 是的,您的架构将起作用.该"$ref": "#"点回模式文档的根.

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

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

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

    2023-02-08 13:40 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有