Recursive JSON Schema

試著忘記壹切 提交于 2019-12-04 04:06:57

问题


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)

At this time I've got this:

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

The problem is that it valid only for the first level of the menu

Any help will be appreciated


回答1:


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

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



回答2:


use definitions and $ref.

Use this online json/schema editor to check your schema visually.

Paste the schema code to the Schema area and click Update Schema.

editor screenshot:

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

schema code:

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


来源:https://stackoverflow.com/questions/24989786/recursive-json-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!