Skip to content

Latest commit

 

History

History
72 lines (44 loc) · 2.36 KB

File metadata and controls

72 lines (44 loc) · 2.36 KB
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).

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.

!!! note

For backward compatibility, OpenAPI 3.1 validation in openapi-core currently accepts OpenAPI 3.0-style format checker behavior, including `byte` and `binary`.

You can also define your own format validators (See 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:

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

The request object should implement the OpenAPI Request protocol (See Integrations).

!!! note

The Webhooks feature is part of OpenAPI v3.1+

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

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

The webhook request object should implement the OpenAPI WebhookRequest protocol (See Integrations).

You can also define your own request validator (See 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:

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

!!! note

The Webhooks feature is part of OpenAPI v3.1+

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

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

You can also define your own response validator (See Response Validator).