OpenAPI 2.0 (aka Swagger) and OpenAPI 3 spec validator
Project description
About
OpenAPI Spec Validator is a Python library that validates OpenAPI Specs against the OpenAPI 2.0 (aka Swagger), OpenAPI 3.0 and OpenAPI 3.1 specification. The validator aims to check for full compliance with the Specification.
Installation
$ pip install openapi-spec-validator
Alternatively you can download the code and install from the repository:
$ pip install -e git+https://github.com/p1c2u/openapi-spec-validator.git#egg=openapi_spec_validator
Usage
Command Line Interface
Straight forward way:
$ openapi-spec-validator openapi.yaml
pipes way:
$ cat openapi.yaml | openapi-spec-validator -
docker way:
$ docker run -v path/to/openapi.yaml:/openapi.yaml --rm p1c2u/openapi-spec-validator /openapi.yaml
or more pythonic way:
$ python -m openapi_spec_validator openapi.yaml
Examples
By default, OpenAPI spec version is detected. To validate spec:
from openapi_spec_validator import validate_spec
from openapi_spec_validator.readers import read_from_filename
spec_dict, spec_url = read_from_filename('openapi.yaml')
# If no exception is raised by validate_spec(), the spec is valid.
validate_spec(spec_dict)
validate_spec({'openapi': '3.1.0'})
Traceback (most recent call last):
...
OpenAPIValidationError: 'info' is a required property
Add spec_url to validate spec with relative files:
validate_spec(spec_dict, spec_url='file:///path/to/spec/openapi.yaml')
You can also validate spec from url:
from openapi_spec_validator import validate_spec_url
# If no exception is raised by validate_spec_url(), the spec is valid.
validate_spec_url('http://example.com/openapi.json')
In order to explicitly validate a:
Swagger / OpenAPI 2.0 spec, import openapi_v2_spec_validator
OpenAPI 3.0 spec, import openapi_v30_spec_validator
OpenAPI 3.1 spec, import openapi_v31_spec_validator
and pass the validator to validate_spec or validate_spec_url function:
validate_spec(spec_dict, validator=openapi_v31_spec_validator)
You can also explicitly import openapi_v3_spec_validator which is a shortcut to the latest v3 release.
If you want to iterate through validation errors:
from openapi_spec_validator import openapi_v3_spec_validator
errors_iterator = openapi_v3_spec_validator.iter_errors(spec)
License
Copyright (c) 2017-2022, Artur Maciag, All rights reserved. Apache v2
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for openapi-spec-validator-0.5.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8248634bad1f23cac5d5a34e193ab36e23914057ca69e91a1ede5af75552c465 |
|
MD5 | 16751892d64baa2f2ff554d60126c065 |
|
BLAKE2b-256 | 6dde97a86134549c5a78ae0492fafdb5667c0c889a5f0d55993d2aa903252203 |
Hashes for openapi_spec_validator-0.5.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a8aee1e45b1ac868e07ab25e18828fe9837baddd29a8e20fdb3d3c61c8eea3d |
|
MD5 | bbef221b50461874b7c9989def352e37 |
|
BLAKE2b-256 | a3c87834420c05d7c256a61d312aa5f86780f07810868cc26c30f4978e397fcb |