递归JSON模式

时间:2014-07-28 06:47:40

标签: json recursion jsonschema

我正在尝试使用子菜单为菜单创建正确的JSON Schema。 所以我应该从item中定义一个应包含三个项目的数组。 1显示名称,2 URL和子项(应该是具有相同结构的对象数组)

此时我已经得到了这个:

{
    "type": "array",
    "additionalProperties": false,  // have no idea what is this for :)
    "items": {
        "type": "object",
        "additionalProperties": false, // have no idea what is this for :)
        "description": "MenuLink",
        "id": "menuLink",
        "properties": {
            "display_name": {
                "type": "string",
                "title": "Link display name",
                "minLength": 2
            },
            "url": {
                "type": "string",
                "title": "URL address",
                "minLength": 2
            },
            "children": {
                "type": "array",
                "title": "Childrens",
                "additionalItems": false,  // have no idea what is this for :)
                "items": {
                    "$ref": "menuLink"
                }
            }
        },
        "required": [
            "display_name",
            "url"
        ]
    }
}

问题是它仅对菜单的第一级有效

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

数组中的additionalProperties什么都不做,它只是被忽略了。对于对象,它不允许任何其他未在'属性中定义的属性

此架构可能有效,也可能无效,具体取决于验证器。参考分辨率是极少数验证器正确执行的最棘手的主题。看看这个:https://github.com/ebdrup/json-schema-benchmark

创建所需内容的更传统方法:

{
  "definitions": {
    "menuLink": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "display_name": {
            "type": "string",
            "title": "Link display name",
            "minLength": 2
        },
        "url": {
            "type": "string",
            "title": "URL address",
            "minLength": 2
        },
        "children": {
            "type": "array",
            "title": "Childrens",
            "items": { "$ref": "#/definitions/menuLink" }
        }
      },
      "required": [ "display_name", "url" ]
    }
  },
  "type": "array",
  "items": { "$ref": "#/definitions/menuLink" }
}

答案 1 :(得分:0)

使用定义和$ ref。

使用此online json/schema editor直观地检查您的架构。

将架构代码粘贴到架构区域,然后点击更新架构

编辑截图:

------------------>>> enter image description here

架构代码:

{
    "definitions": {
        "MenuItem": {
            "title": "MenuItem",
            "properties": {
                "display_name": {
                    "type": "string",
                    "title": "Link display name",
                    "minLength": 2
                },
                "url": {
                    "type": "string",
                    "title": "URL address",
                    "minLength": 2
                },
                "children": {
                    "type": "array",
                    "title": "Children",
                    "items": {
                        "$ref": "#/definitions/MenuItem"
                    }
                }
            }
        }
    },
    "title": "MenuItems",
    "type": "array",
    "items": {
        "$ref": "#/definitions/MenuItem"
    }
}