JSON Schema Exemple de notation de schéma JSON pour valider des clés arbitraires et des types de dictionnaire qui valorisent des chaînes ou des nombres.
JSON-schema
{
"type": "object",
"additionalProperties": {
"anyOf": [
{"type": "string"},
{"type": "number"},
],
},
}
Example@python Un exemple d'exécution du schéma ci-dessus sur Python à l'aide du module jsonschema.
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
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