diff --git a/README.rst b/README.rst index 3182be6..773576e 100644 --- a/README.rst +++ b/README.rst @@ -128,6 +128,34 @@ and unmarshal response data from validation result # get data validated_data = result.data +or use shortcuts for simple validation + +.. code-block:: python + + from openapi_core import validate_data + + validated_data = validate_data(spec, request, response) + +Response object should implement BaseOpenAPIResponse interface. You can use FlaskOpenAPIResponse a Flask/Werkzeug response wrapper implementation: + +.. code-block:: python + + from openapi_core.validators import ResponseValidator + from openapi_core.wrappers import FlaskOpenAPIResponse + + openapi_response = FlaskOpenAPIResponse(flask_response) + validator = ResponseValidator(spec) + result = validator.validate(openapi_request, openapi_response) + +or specify response wrapper class for shortcuts + +.. code-block:: python + + from openapi_core import validate_parameters, validate_body + + validated_data = validate_data( + spec, request, response, response_wrapper_class=FlaskOpenAPIResponse) + Related projects ================ * `openapi-spec-validator `__ diff --git a/openapi_core/shortcuts.py b/openapi_core/shortcuts.py index 555dd07..2b0bedc 100644 --- a/openapi_core/shortcuts.py +++ b/openapi_core/shortcuts.py @@ -5,8 +5,8 @@ from openapi_spec_validator import default_handlers from openapi_core.exceptions import OpenAPIParameterError, OpenAPIBodyError from openapi_core.specs import SpecFactory -from openapi_core.validators import RequestValidator -from openapi_core.wrappers import FlaskOpenAPIRequest +from openapi_core.validators import RequestValidator, ResponseValidator +from openapi_core.wrappers import FlaskOpenAPIRequest, FlaskOpenAPIResponse def create_spec(spec_dict, spec_url=''): @@ -43,3 +43,20 @@ def validate_body(spec, request, wrapper_class=FlaskOpenAPIRequest): return result.body else: return result.body + + +def validate_data( + spec, request, response, + request_wrapper_class=FlaskOpenAPIRequest, + response_wrapper_class=FlaskOpenAPIResponse): + if request_wrapper_class: + request = request_wrapper_class(request) + if response_wrapper_class: + response = response_wrapper_class(response) + + validator = ResponseValidator(spec) + result = validator.validate(request, response) + + result.raise_for_errors() + + return result.data