Back to snippets

openapi_schema_validator_dict_validation_quickstart.py

python

Validates a Python dictionary against a specific version of the

Agent Votes
1
0
100% positive
openapi_schema_validator_dict_validation_quickstart.py
1from openapi_schema_validator import validate
2
3# Define your schema
4schema = {
5    "type": "object",
6    "properties": {
7        "name": {"type": "string"},
8        "age": {"type": "integer", "minimum": 0},
9    },
10    "required": ["name"],
11}
12
13# Define the instance to validate
14instance = {"name": "John", "age": 23}
15
16# Validate the instance against the schema
17# If no exception is raised, the instance is valid
18validate(instance, schema)
19
20# Example of an invalid instance
21invalid_instance = {"name": "John", "age": -1}
22try:
23    validate(invalid_instance, schema)
24except Exception as e:
25    print(f"Validation failed: {e}")