File: validation.rst

package info (click to toggle)
python-openapi-core 0.19.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,008 kB
  • sloc: python: 18,868; makefile: 47
file content (66 lines) | stat: -rw-r--r-- 2,208 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
58
59
60
61
62
63
64
65
66
Validation
==========

Validation is a process to validate request/response data under a given schema defined in OpenAPI specification.

Additionally, openapi-core uses the ``format`` keyword to check if primitive types conform to defined formats.

Such valid formats can be forther unmarshalled (See :doc:`unmarshalling`).

Depends on the OpenAPI version, openapi-core comes with a set of built-in format validators such as: ``date``, ``date-time``, ``binary``, ``uuid`` or ``byte``.

You can also define your own format validators (See :doc:`customizations/extra_format_validators`).

Request validation
------------------

Use ``validate_request`` method to validate request data against a given spec. By default, OpenAPI spec version is detected:

.. code-block:: python

    # raises error if request is invalid
    openapi.validate_request(request)

Request object should implement OpenAPI Request protocol (See :doc:`integrations/index`).

.. note::

    Webhooks feature is part of OpenAPI v3.1 only

Use the same method to validate webhook request data against a given spec.

.. code-block:: python

    # raises error if request is invalid
    openapi.validate_request(webhook_request)

Webhook request object should implement OpenAPI WebhookRequest protocol (See :doc:`integrations/index`).

You can also define your own request validator (See :doc:`customizations/request_validator_cls`).

Response validation
-------------------

Use ``validate_response`` function to validate response data against a given spec. By default, OpenAPI spec version is detected:

.. code-block:: python

    from openapi_core import validate_response

    # raises error if response is invalid
    openapi.validate_response(request, response)

Response object should implement OpenAPI Response protocol  (See :doc:`integrations/index`).

.. note::

    Webhooks feature is part of OpenAPI v3.1 only

Use the same function to validate response data from webhook request against a given spec.

.. code-block:: python

    # raises error if request is invalid
    openapi.validate_response(webhook_request, response)

You can also define your own response validator (See :doc:`customizations/response_validator_cls`).