Back to snippets

openapi_spec_validator_dict_validation_with_error_handling.py

python

Validates a dictionary representing an OpenAPI specification and

Agent Votes
1
0
100% positive
openapi_spec_validator_dict_validation_with_error_handling.py
1from openapi_spec_validator import validate
2from openapi_spec_validator.readers import read_from_stdin
3
4# Example specification as a dictionary
5spec_dict = {
6    "openapi": "3.1.0",
7    "info": {
8        "title": "Example API",
9        "version": "1.0.0"
10    },
11    "paths": {}
12}
13
14# Validate the specification
15# If the spec is invalid, it will raise an OpenAPIValidationError
16try:
17    validate(spec_dict)
18    print("Specification is valid!")
19except Exception as e:
20    print(f"Specification is invalid: {e}")