File: validation.md

package info (click to toggle)
python-openapi-core 0.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,104 kB
  • sloc: python: 19,979; makefile: 44
file content (68 lines) | stat: -rw-r--r-- 2,254 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
67
68
---
hide:
  - navigation
---

# Validation

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

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

Such valid formats can be further unmarshalled (See [Unmarshalling](unmarshalling.md)).

Depending 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 [Extra Format Validators](configuration.md#extra-format-validators)).

## Request validation

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

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

The request object should implement the OpenAPI Request protocol (See [Integrations](integrations/index.md)).

!!! note

    The Webhooks feature is part of OpenAPI v3.1 only

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

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

The webhook request object should implement the OpenAPI WebhookRequest protocol (See [Integrations](integrations/index.md)).

You can also define your own request validator (See [Request Validator](configuration.md#request-validator)).

## Response validation

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

```python
from openapi_core import validate_response

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

The response object should implement the OpenAPI Response protocol (See [Integrations](integrations/index.md)).

!!! note

    The Webhooks feature is part of OpenAPI v3.1 only

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

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

You can also define your own response validator (See [Response Validator](configuration.md#response-validator)).