OpenAPI Schema Validator
openapi-schema-validator is a Python library designed for validating data instances against OpenAPI Schema Specification versions 3.0, 3.1, and 3.2. It leverages `jsonschema` under the hood and provides specific validators for different OpenAPI versions, along with features for handling read/write contexts and managing external references. The library is actively maintained, with version 0.8.1 being the latest, and releases occur as new OpenAPI specifications emerge or features/fixes are required.
Warnings
- breaking Remote `$ref` resolution is now disabled by default. The `validate` function and validator classes use a local-only empty registry to avoid implicit remote `$ref` retrieval.
- breaking For `OAS30Validator` and `OAS30StrictValidator`, a schema type 'string' now *only* accepts Python `str` instances. It no longer accepts `bytes`.
- breaking Support for Python 3.8 and 3.9 has been dropped.
- breaking Support for Python 3.7 has been dropped.
- breaking `OAS30Validator` no longer accepts `read` and `write` properties directly. These were removed to align with a clearer read/write context model.
- gotcha The argument order for the `validate` function is crucial: `validate(instance, schema)`, not `validate(schema, instance)`. Incorrect order will lead to validation errors or unexpected behavior.
- gotcha The library validates against a provided schema object; it does not automatically load OpenAPI documents from file paths. You must load your OpenAPI document (e.g., from YAML or JSON) into a Python dictionary or object first.
Install
-
pip install openapi-schema-validator
Imports
- validate
from openapi_schema_validator import validate
- OAS32Validator
from openapi_schema_validator import OAS32Validator
- OAS31Validator
from openapi_schema_validator import OAS31Validator
- OAS30Validator
from openapi_schema_validator import OAS30Validator
- OAS30StrictValidator
from openapi_schema_validator import OAS30StrictValidator
- OAS30ReadValidator
from openapi_schema_validator import OAS30ReadValidator
- OAS30WriteValidator
from openapi_schema_validator import OAS30WriteValidator
Quickstart
from openapi_schema_validator import validate
# A sample OpenAPI 3.2 schema
schema = {
"type": "object",
"required": ["name"],
"properties": {
"name": {"type": "string"},
"age": {
"type": ["integer", "null"],
"format": "int32",
"minimum": 0,
},
"birth-date": {"type": "string", "format": "date"},
"address": {
"type": "array",
"prefixItems": [
{"type": "number"},
{"type": "string"},
{"enum": ["Street", "Avenue", "Boulevard"]},
{"enum": ["NW", "NE", "SW", "SE"]}
],
"items": False,
}
},
"additionalProperties": False,
}
# A valid instance
try:
validate({"name": "John", "age": 23, "address": [1600, "Pennsylvania", "Avenue"]}, schema)
print("Instance is valid!")
except Exception as e:
print(f"Validation failed: {e}")
# An invalid instance (missing required 'name')
try:
validate({"age": 23}, schema)
except Exception as e:
print(f"Validation failed as expected: {e}")
# An invalid instance (additional property 'city')
try:
validate({"name": "John", "city": "London"}, schema)
except Exception as e:
print(f"Validation failed as expected: {e}")