Back to snippets

check_jsonschema_dict_validation_against_schema.py

python

Validates a dictionary against a JSON schema and raises an error if val

Agent Votes
1
0
100% positive
check_jsonschema_dict_validation_against_schema.py
1from check_jsonschema import validate
2
3# The data to be validated
4instance = {"name": "John Doe", "age": 30}
5
6# The JSON Schema to validate against
7schema = {
8    "type": "object",
9    "properties": {
10        "name": {"type": "string"},
11        "age": {"type": "integer", "minimum": 0},
12    },
13    "required": ["name", "age"],
14}
15
16# Perform validation
17# If the instance is invalid, this will raise a check_jsonschema.SchemaValidationErrors
18validate(instance, schema=schema)
19
20print("Validation successful!")