EN VI

Double nested JSON schema?

2024-03-12 00:30:05
How to Double nested JSON schema

I have one top-level schema content.json

{
    "title": "Program content schema",
    "$id": "https://example.com/schemas/program",
    "type": "object",
    "properties": {
        "content": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "string"
                },
                "tools": {
                    "$ref": "/schemas/tool"
                }
            }
        }
    }
}

which references another schema, tool.json

{
  "title": "Tool information schema",
    "$id": "https://example.com/schemas/tool",
    "type": "object",
    "properties": {
        "nodeInformation": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string"
                },
                "description": {
                    "type": "string"
                },
                "$ref": "/schemas/gui"
            }
        }
    }
}

and that references another schema, gui.json

{
    "title": "Gui schema",
    "$id": "https://example.com/schemas/gui",
    "type": "object",
    "properties": {
      "active": {
            "type": "boolean"
        },
        "children": {
            "type": "array"
        }
    }
}

and finally the data itself

{
    "content": {
        "id": "",
        "tools": {
            "nodeInformation": {
                "name": "Dummy Name",
                "description": "Tool Description"
            },
            "active": "true",
            "children": []
        }
    }
}

If I introduce errors in the gui schema, they are not recognized. For example, the following data should have been recognized as an error

"active": "true"

because the schema defines it as

"active": {
    "type": "boolean"
}

Solution:

The tool schema needs an allOf to compose your defined schemas

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "Program content schema",
    "$id": "https://example.com/schemas/program",
    "type": "object",
    "properties": {
        "content": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "string"
                },
                "tools": {
                    "$ref": "/schemas/tool"
                }
            }
        }
    }
}
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "Tool information schema",
    "$id": "https://example.com/schemas/tool",
    "allOf": [
        {
            "type": "object",
            "properties": {
                "nodeInformation": {
                    "type": "object",
                    "properties": {
                        "name": {
                            "type": "string"
                        },
                        "description": {
                            "type": "string"
                        }
                    }
                }
            }
        },
        {
            "$ref": "/schemas/gui"
        }
    ]
}

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "Gui schema",
    "$id": "https://example.com/schemas/gui",
    "type": "object",
    "properties": {
        "active": {
            "type": "boolean"
        },
        "children": {
            "type": "array"
        }
    }
}
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login