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.
pip install openapi-schema-validatorVerified import paths — ran on the pinned version, not inferred.
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).
To resolve external references, pass an explicit `registry` (e.g., from `referencing`). Set `allow_remote_references=True` only if you explicitly accept `jsonschema`'s default remote retrieval behavior, especially if dealing with untrusted sources.
Ensure that string-typed properties intended to carry binary data in OpenAPI 3.0 are handled as Python `str` (e.g., base64-encoded) before validation, or use appropriate media type modeling for raw binary payloads for OpenAPI 3.1+.
Upgrade your Python environment to 3.10 or newer (requires_python: >=3.10.0, <4.0.0).
Upgrade your Python environment to 3.8 or newer. (Note: 3.8/3.9 were later dropped in 0.7.0, so aim for 3.10+).
For OpenAPI 3.0 validation in specific read/write contexts, use `OAS30ReadValidator` or `OAS30WriteValidator` instead.
Always pass the data instance as the first argument and the OpenAPI schema as the second argument to `validate`.
Load your OpenAPI document into a Python dictionary using a library like `yaml` or `json` before passing it to `validate` or a specific validator.
Install the library using pip: `pip install openapi-schema-validator`
Ensure the data instance includes all properties listed in the 'required' array of the corresponding schema.
Reverse the order of arguments to `validate(instance, schema)`.
Ensure 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.