Skip to main content

Tornado OpenAPI 3 request and response validation library

Project description

https://travis-ci.com/correl/tornado-openapi3.svg?branch=master

Tornado OpenAPI 3 request and response validation library.

Provides integration between the Tornado web framework and Openapi-core library for validating request and response objects against an OpenAPI 3 specification.

Adding validation to request handlers

from openapi_core import create_spec  # type: ignore
from openapi_core.exceptions import OpenAPIError  # type: ignore
from openapi_core.deserializing.exceptions import DeserializeError  # type: ignore
from openapi_core.schema.media_types.exceptions import (  # type: ignore
    InvalidContentType,
)
from openapi_core.unmarshalling.schemas.exceptions import ValidateError  # type: ignore
from tornado.web import RequestHandler
from tornado_openapi3.requests import RequestValidator
import yaml


class OpenAPIRequestHandler(RequestHandler):
    async def prepare(self) -> None:
        maybe_coro = super().prepare()
        if maybe_coro and asyncio.iscoroutine(maybe_coro):  # pragma: no cover
            await maybe_coro

        spec = create_spec(yaml.safe_load(self.render_string("openapi.yaml")))
        validator = RequestValidator(spec)
        result = validator.validate(self.request)
        try:
            result.raise_for_errors()
        except InvalidContentType:
            self.set_status(415)
            self.finish()
        except (DeserializeError, ValidateError) as e:
            self.set_status(400)
            self.finish()
        except OpenAPIError:
            raise

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tornado-openapi3-0.1.0.tar.gz (2.6 kB view hashes)

Uploaded Source

Built Distributions

tornado_openapi3-0.1.0-py3-none-any.whl (2.9 kB view hashes)

Uploaded Python 3

tornado_openapi3-0.1.0-1-py3-none-any.whl (3.5 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page