jsonsystems / json-schema

JSONSchema.Net Public Repository
Apache License 2.0
663 stars 64 forks source link

Dynamic keys #105

Closed hasethuraman closed 3 years ago

hasethuraman commented 3 years ago

Hi,

We use hostname as dictionary key. Is there a way to achieve using existing schema? Please suggest where to submit this improvement otherwise.

jackwootton commented 3 years ago

@mailhari Thanks for posting the issue, I'm unsure what you mean. Do you mean your JSON (input) is

{
    "hostname": "foo"
}

This will generate something similar to

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "title": "The root schema",
    "description": "The root schema comprises the entire JSON document.",
    "default": {},
    "examples": [
        {
            "hostname": "foo"
        }
    ],
    "required": [
        "hostname"
    ],
    "properties": {
        "hostname": {
            "$id": "#/properties/hostname",
            "type": "string",
            "title": "The hostname schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "foo"
            ]
        }
    },
    "additionalProperties": true
}
hasethuraman commented 3 years ago

@jsonsystems Thanks for getting back.

{ "server-01": { "memory": "2GB", "cores": 2 }, "server-02": { "memory": "2GB", "cores": 2 } ... }

So the keys here server-01 and server-02 are dynamic and cannot be hardcoded in $id.

jackwootton commented 3 years ago

@mailhari Thank you for the example, can you point out what is wrong with the schema generated below?

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "title": "The root schema",
    "description": "The root schema comprises the entire JSON document.",
    "default": {},
    "required": [
        "server-01",
        "server-02"
    ],
    "properties": {
        "server-01": {
            "$id": "#/properties/server-01",
            "type": "object",
            "title": "The server-01 schema",
            "description": "An explanation about the purpose of this instance.",
            "default": {},
            "required": [
                "memory",
                "cores"
            ],
            "properties": {
                "memory": {
                    "$id": "#/properties/server-01/properties/memory",
                    "type": "string",
                    "title": "The memory schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": ""
                },
                "cores": {
                    "$id": "#/properties/server-01/properties/cores",
                    "type": "integer",
                    "title": "The cores schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": 0
                }
            },
            "additionalProperties": true
        },
        "server-02": {
            "$id": "#/properties/server-02",
            "type": "object",
            "title": "The server-02 schema",
            "description": "An explanation about the purpose of this instance.",
            "default": {},
            "required": [
                "memory",
                "cores"
            ],
            "properties": {
                "memory": {
                    "$id": "#/properties/server-02/properties/memory",
                    "type": "string",
                    "title": "The memory schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": ""
                },
                "cores": {
                    "$id": "#/properties/server-02/properties/cores",
                    "type": "integer",
                    "title": "The cores schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": 0
                }
            },
            "additionalProperties": true
        }
    },
    "additionalProperties": true
}
hasethuraman commented 3 years ago

I wanted to use the same schema (without modifying) when I add a new "server-03". Basically the above schema doesnt accept other than server-01 and 02 right?

jackwootton commented 3 years ago

@mailhari Ah, I think your question has more to do with the JSON Schema specification than www.jsonschema.net. I recommend heading over to http://json-schema.slack.com and asking your question there.

hasethuraman commented 3 years ago

Thanks @jsonsystems . Will take it there.