递归 JSON 模式

Dim*_*hov 5 recursion json jsonschema

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

这个时候我有这个:

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

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

任何帮助将不胜感激

esp*_*esp 2

数组中的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" }
}
Run Code Online (Sandbox Code Playgroud)