递归JSON模式 [英] Recursive JSON Schema

查看:208
本文介绍了递归JSON模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为带有子菜单的菜单创建正确的JSON模式. 因此,我应该从应该定义三个项目的项目中定义一个数组. 1个显示名称,2个URL和子级(应该是具有相同结构的对象数组)

I'm trying to create proper JSON Schema for menu with sub-menus. So I should define an array from item which should contain three items. 1 Display name, 2 URL and Children (which should be an array of object with the same structure)

这时候我有这个:

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

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

任何帮助将不胜感激

推荐答案

additionalProperties不会执行任何操作,只是将其忽略即可.对于对象,不允许使用属性"中未定义的其他任何属性

additionalProperties in arrays does nothing, it is simply ignored. For object it doesn't allow any other properties that are not defined in 'properties'

根据验证者的不同,该模式可能会也可能不会起作用.参考解析是最棘手的主题,很少有验证者能够正确执行.看看这个: https://github.com/ebdrup/json-schema-benchmark

This schema may or may not work depending on the validator. Reference resolution is the trickiest subject that very few validators do correctly. Check out this: https://github.com/ebdrup/json-schema-benchmark

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

The more traditional approach to creating what you want:

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

这篇关于递归JSON模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆