Response validated_data shortcut

This commit is contained in:
Artur Maciag 2017-11-06 14:32:46 +00:00
parent fda4615057
commit b9a8477cd5
2 changed files with 47 additions and 2 deletions

View file

@ -128,6 +128,34 @@ and unmarshal response data from validation result
# get data # get data
validated_data = result.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 Related projects
================ ================
* `openapi-spec-validator <https://github.com/p1c2u/openapi-spec-validator>`__ * `openapi-spec-validator <https://github.com/p1c2u/openapi-spec-validator>`__

View file

@ -5,8 +5,8 @@ from openapi_spec_validator import default_handlers
from openapi_core.exceptions import OpenAPIParameterError, OpenAPIBodyError from openapi_core.exceptions import OpenAPIParameterError, OpenAPIBodyError
from openapi_core.specs import SpecFactory from openapi_core.specs import SpecFactory
from openapi_core.validators import RequestValidator from openapi_core.validators import RequestValidator, ResponseValidator
from openapi_core.wrappers import FlaskOpenAPIRequest from openapi_core.wrappers import FlaskOpenAPIRequest, FlaskOpenAPIResponse
def create_spec(spec_dict, spec_url=''): def create_spec(spec_dict, spec_url=''):
@ -43,3 +43,20 @@ def validate_body(spec, request, wrapper_class=FlaskOpenAPIRequest):
return result.body return result.body
else: else:
return result.body 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