Need help with openapi-spec-validator?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

p1c2u
185 Stars 35 Forks Apache License 2.0 177 Commits 38 Opened issues

Description

OpenAPI Spec validator

Services available

!
?

Need anything else?

Contributors list


OpenAPI Spec validator


.. image:: https://img.shields.io/pypi/v/openapi-spec-validator.svg :target: https://pypi.python.org/pypi/openapi-spec-validator .. image:: https://travis-ci.org/p1c2u/openapi-spec-validator.svg?branch=master :target: https://travis-ci.org/p1c2u/openapi-spec-validator .. image:: https://img.shields.io/codecov/c/github/p1c2u/openapi-spec-validator/master.svg?style=flat :target: https://codecov.io/github/p1c2u/openapi-spec-validator?branch=master .. image:: https://img.shields.io/pypi/pyversions/openapi-spec-validator.svg :target: https://pypi.python.org/pypi/openapi-spec-validator .. image:: https://img.shields.io/pypi/format/openapi-spec-validator.svg :target: https://pypi.python.org/pypi/openapi-spec-validator .. image:: https://img.shields.io/pypi/status/openapi-spec-validator.svg :target: https://pypi.python.org/pypi/openapi-spec-validator

About

OpenAPI Spec Validator is a Python library that validates OpenAPI Specs against the

OpenAPI 2.0 (aka
Swagger) 
__ and
OpenAPI
3.0 
__ 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:

.. code-block:: bash

$ pip install -e git+https://github.com/p1c2u/openapi-spec-validator.git#egg=openapispecvalidator

Usage

Command Line Interface


Straight forward way:

.. code:: bash

$ openapi-spec-validator openapi.yaml

pipes way:

.. code:: bash

$ cat openapi.yaml | openapi-spec-validator -

docker way:

.. code:: bash

$ docker run -v path/to/openapi.yaml:/openapi.yaml --rm p1c2u/openapi-spec-validator /openapi.yaml

or more pythonic way:

.. code:: bash

$ python -m openapi_spec_validator openapi.yaml

Examples


By default, OpenAPI v3 syntax is expected. To validate an OpenAPI v3 spec:

.. code:: python

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({})

Traceback (most recent call last): ... OpenAPIValidationError: 'openapi' is a required property

In order to validate a Swagger / OpenAPI 2.0 spec file, import

validate_v2_spec
instead of
validate_spec
. You can also explicitly import
validate_v3_spec
if you want to disambiguate the expected version.

Add

spec_url
to validate spec with relative files:

.. code:: python

validate_spec(spec_dict, spec_url='file:///path/to/spec/openapi.yaml')

You can also validate spec from url:

.. code:: python

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')

If you want to iterate through validation errors:

.. code:: python

from openapi_spec_validator import openapi_v3_spec_validator

errors_iterator = openapi_v3_spec_validator.iter_errors(spec)

Related projects

  • openapi-core 
    __ Python library that adds client-side and server-side support for the OpenAPI.
  • openapi-schema-validator 
    __ Python library that validates schema against the OpenAPI Schema Specification v3.0.

License

Copyright (c) 2017-2021, Artur Maciag, All rights reserved. Apache v2

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.