When using Python jsonschema it is possible to define schemas and instances that cannot be expressed in valid JSON.
>>> import jsonschema
>>> schema = {
... "type": "object",
... "properties": {"1": {}, 2:{}},
... "additionalProperties": False
... }
Now
>>> jsonschema.validate({"1": "spam", 2: "eggs"}, schema)
does not raise an exception, while the code below fails:
>>> jsonschema.validate({1: "spam"}, schema)
Traceback (most recent call last):
...
jsonschema.exceptions.ValidationError: Additional properties are not allowed (1 was unexpected)
Failed validating 'additionalProperties' in schema:
{'additionalProperties': False,
'properties': {2: {}, '1': {}},
'type': 'object'}
On instance:
{1: 'spam'}
I'm a little confused here: the Python mapping {"1": "spam", 2: "eggs"}
cannot be serialised in a valid JSON object, and the same applies to the schema
mapping above. (In JSON objects are name/value mapping where the name has to be a string, and cannot be an integer or another data type).
Is this intended behaviour, i.e. the jsonschema semantics is extended to include more general python data types, or is the above use of schema
invalid and should be flagged as an error by the jsonschema library? I read the docs, but was not able to find a mention to this point.