5

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

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
  • Have look here: http://stackoverflow.com/questions/20752716/json-schema-recursive-schema-definition – jruizaranguren Jul 30 '14 at 10:13
  • `additionalProperties: false` means that a json file may only contain the properties defined in the `properties` or in the `patternProperties` section of your schema. Other properties won't be accepted. See also here http://json-schema.org/latest/json-schema-validation.html#anchor64 – Jan Wy Jul 31 '14 at 09:17

2 Answers2

2

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" }
}
esp
  • 7,314
  • 6
  • 49
  • 79
  • 1
    I like this answer, but `additionalItems` is meaningless in this schema. `additionalItems` only has meaning when `items` is an array of schemas instead of a single schema. http://json-schema.org/latest/json-schema-validation.html#anchor37 – Jason Desrosiers Jun 21 '15 at 23:56
  • @Jason thanks, that's correct, removed. I guess I've just copied it from the question :) – esp Jun 22 '15 at 10:27
0

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:

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

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"
    }
}
cuixiping
  • 24,167
  • 8
  • 82
  • 93