Registry /
http-networking / 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.
Install & Compatibility
Where this runs
tested against v0.9.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.925 runs
installs and imports cleanly · install 0.0s · import 0.810s · 32.3MB
glibcpy 3.10–3.925 runs
installs and imports cleanly · install 3.7s · import 0.721s · 32MB
28MB installed
● package 28MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
validate
✓ from openapi_schema_validator import validate
High-level function for validating an instance against a schema. Defaults to OAS32Validator.
OAS32Validator
✓ from openapi_schema_validator import OAS32Validator
Use for OpenAPI 3.2 schema validation (default for `validate` in recent versions).
OAS31Validator
✓ from openapi_schema_validator import OAS31Validator
Use for OpenAPI 3.1 schema validation.
OAS30Validator
✓ from openapi_schema_validator import OAS30Validator
Use for OpenAPI 3.0 schema validation.
OAS30StrictValidator
✓ from openapi_schema_validator import OAS30StrictValidator
Use for strict OpenAPI 3.0 schema validation, enforcing stricter typing (e.g., 'string' only accepts `str`).
OAS30ReadValidator
✓ from openapi_schema_validator import OAS30ReadValidator
Use for OpenAPI 3.0 schema validation specifically in a read context (honoring 'readOnly' keywords).
OAS30WriteValidator
✓ from openapi_schema_validator import OAS30WriteValidator
Use for OpenAPI 3.0 schema validation specifically in a write context (honoring 'writeOnly' keywords).
The simplest way to validate an instance against an OpenAPI schema is to use the `validate` function. Provide the instance to be validated and the OpenAPI schema object. By default, it expects the latest OpenAPI schema syntax (3.2).
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}")
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'openapi_schema_validator'
The 'openapi-schema-validator' Python package is not installed in the active Python environment.
fixInstall the library using pip: `pip install openapi-schema-validator`
ValidationError: 'propertyName' is a required property
The data instance being validated is missing a property that is marked as 'required' in the OpenAPI schema.
fixEnsure the data instance includes all properties listed in the 'required' array of the corresponding schema.
Incorrect argument order for validate()
The `validate` function was called with the schema as the first argument and the instance as the second argument, instead of `validate(instance, schema)` as required.
fixReverse the order of arguments to `validate(instance, schema)`.
AttributeError: 'list' object has no attribute 'get'
The validator received a data instance of an unexpected type (e.g., a list) where a dictionary-like object was anticipated, leading to an attempt to call a dictionary method like `.get()` on an incompatible type.
fixEnsure the data instance conforms to the expected type (e.g., an object/dictionary) as defined by the OpenAPI schema before passing it to the validator.
Audit
Dependencies
jsonschemarequiredCore dependency for schema validation logic.
referencingoptionalUsed for explicit external JSON Schema reference resolution.
jsonschema-specificationsrequiredCompatibility dependency; version limits were removed in 0.6.3.