0

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.

8
  • By default you can add anything to an object if, only when you specify additionalProperties: false this is not true. Maybe I'm misunderstanding Commented Sep 19, 2021 at 11:10
  • @Evert are you saying that with the schema I've described at the top of the message, if I validate against the data example I've also shown... it will validate OK? Commented Sep 19, 2021 at 13:23
  • you mean you havent tried it? Commented Sep 19, 2021 at 16:49
  • @Evert Not yet... I need to get the test data generator going but by EOD I should Commented Sep 19, 2021 at 19:33
  • @Evert - Works, thanks for the insight and Ether for expanding and including the link. Commented Sep 19, 2021 at 20:39

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.