mirror of
https://github.com/correl/openapi-core.git
synced 2024-11-28 19:19:52 +00:00
Response validated_data shortcut
This commit is contained in:
parent
fda4615057
commit
b9a8477cd5
2 changed files with 47 additions and 2 deletions
28
README.rst
28
README.rst
|
@ -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>`__
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue