Search code examples
jsonschema

How to indicate dynamic additions support in json schema?


Just started with jsonschema. I want to describe a collection of objects for a property where the key may not be known a priori.

Here is my starting point:

                "tx_properties": {
                    "type": "object",
                    "anyOf": [
                        {
                            "required": [
                                "original_msg"
                            ]
                        }
                    ],
                    "properties": {
                        "original_msg": {
                            "type": "string"
                        }
                    }
                }
            }

I want to be able to validate the additions of more properties for tx_properties that may have different types but are not known at schema definition time.

For example I might have, in json:

"tx_properties": {
    "original_msg": "foo",
    "something_else": "bar",
    "or_something_numeric": 172,
    "or_even_deeper_things": {
        "fungible": false,
    }
}

As a n00b I'm a bit stuck on how to accomplish this. The use of anyOne is what I thought I needed at least in the final solution.


Solution

  • As @Evert said, "additionalProperties": false can be used to indicate that no other properties other than those listed in properties keywords (and patternProperties) are permitted. If additionalProperties is omitted, the behaviour is as if the schema said "additionalProperties": true (that is, additional properties are permitted).

    Also note that the value at additionalProperties doesn't have to be a boolean: it can be a subschema itself, to allow you to conditionally allow for additional properties depending on their value(s).

    Reference: https://json-schema.org/understanding-json-schema/reference/object.html#additional-properties