[PYTHON] A memorandum of JSON Schema notation for dictionaries that take arbitrary keys

JSON Schema JSON schema notation example for validating arbitrary keys and dictionary types that value strings or numbers.

JSON-schema


{
    "type": "object",
    "additionalProperties": {
        "anyOf": [
            {"type": "string"},
            {"type": "number"},
        ],
    },
}

Example@python An example of running the above schema on Python using the jsonschema module.

import jsonschema

schema = {
    "type": "object",
    "additionalProperties": {
        "anyOf": [
            {"type": "string"},
            {"type": "number"},
        ],
    },
}

valid_obj = {
    "key_a" : "value",
    "key_b" : 1,
    "key_c" : 1.1,
}
invalid_obj = {
    "key_a" : "value",
    "key_b" : 1,
    "key_c" : None,
}

jsonschema.validate(valid_obj, schema)

try:
    jsonschema.validate(invalid_obj, schema)
except jsonschema.ValidationError as e:
    print(e)
None is not valid under any of the given schemas

Failed validating 'anyOf' in schema['additionalProperties']:
    {'anyOf': [{'type': 'string'}, {'type': 'number'}]}

On instance['key_c']:
    None

reference

Dictionary-like JSON schema - Stack Overflow http://stackoverflow.com/questions/27357861/dictionary-like-json-schema

python - JSON schema validation with arbitrary keys - Stack Overflow http://stackoverflow.com/questions/16081118/json-schema-validation-with-arbitrary-keys

Recommended Posts

A memorandum of JSON Schema notation for dictionaries that take arbitrary keys
A memorandum of speed of arbitrary degree diagonalization
Is your dll a dll that can take advantage of multi-core?
[Django] A collection of scripts that are convenient for development
A memorandum of kernel compilation
A small memorandum of openpyxl
A memorandum of using eigen3
A memorandum of understanding for the Python package management tool ez_setup