Recursive JSON Schema

前端 未结 2 1573
鱼传尺愫
鱼传尺愫 2021-01-20 00:01

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

相关标签:
2条回答
  • 2021-01-20 00:24

    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" }
    }
    
    0 讨论(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:

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

    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"
        }
    }
    
    0 讨论(0)
提交回复
热议问题