【问题标题】:Recursive JSON Schema递归 JSON 模式
【发布时间】:2014-09-19 07:38:36
【问题描述】:

我正在尝试为带有子菜单的菜单创建正确的 JSON 架构。 所以我应该从包含三个项目的项目中定义一个数组。 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"
        ]
    }
}

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

任何帮助将不胜感激

【问题讨论】:

标签: json recursion jsonschema


【解决方案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" }
}

【讨论】:

  • 我喜欢这个答案,但 additionalItems 在这个模式中毫无意义。 additionalItems 仅在 items 是模式数组而不是单个模式时才有意义。 json-schema.org/latest/json-schema-validation.html#anchor37
  • @Jason 谢谢,这是正确的,已删除。我想我刚刚从问题中复制了它:)
【解决方案2】:

使用定义和 $ref。

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

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

编辑器截图:

------------------>>>

架构代码:

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

【讨论】:

    猜你喜欢
    • 2014-01-12
    • 1970-01-01
    • 2014-08-30
    • 2018-02-05
    • 1970-01-01
    • 2012-09-20
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多