File: python.rst

package info (click to toggle)
python-openapi-spec-validator 0.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 772 kB
  • sloc: python: 2,050; makefile: 54
file content (57 lines) | stat: -rw-r--r-- 1,575 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Python package
==============

By default, OpenAPI spec version is detected. To validate spec:

.. code:: python

    from openapi_spec_validator import validate
    from openapi_spec_validator.readers import read_from_filename

    spec_dict, base_uri = read_from_filename('openapi.yaml')

    # If no exception is raised by validate(), the spec is valid.
    validate(spec_dict)

    validate({'openapi': '3.1.0'})

    Traceback (most recent call last):
    ...
    OpenAPIValidationError: 'info' is a required property
         
Add ``base_uri`` to validate spec with relative files:

.. code:: python

    validate(spec_dict, base_uri='file:///path/to/spec/openapi.yaml')

You can also validate spec from url:

.. code:: python

    from openapi_spec_validator import validate_url

    # If no exception is raised by validate_url(), the spec is valid.
    validate_url('http://example.com/openapi.json')

In order to explicitly validate a:

* Swagger / OpenAPI 2.0 spec, import ``OpenAPIV2SpecValidator``
* OpenAPI 3.0 spec, import ``OpenAPIV30SpecValidator`` 
* OpenAPI 3.1 spec, import ``OpenAPIV31SpecValidator`` 

and pass the validator class to ``validate`` or ``validate_url`` function:

.. code:: python

    validate(spec_dict, cls=OpenAPIV31SpecValidator)

You can also explicitly import ``OpenAPIV3SpecValidator`` which is a shortcut to the latest v3 release.

If you want to iterate through validation errors:

.. code:: python

    from openapi_spec_validator import OpenAPIV31SpecValidator

    errors_iterator = OpenAPIV31SpecValidator(spec).iter_errors()