diff --git a/openapi_core/__init__.py b/openapi_core/__init__.py index ea1b642..524ed33 100644 --- a/openapi_core/__init__.py +++ b/openapi_core/__init__.py @@ -1,5 +1,7 @@ """OpenAPI core module""" -from openapi_core.shortcuts import create_spec +from openapi_core.shortcuts import ( + create_spec, validate_parameters, validate_body, +) __author__ = 'Artur MaciÄ…g' __email__ = 'maciag.artur@gmail.com' @@ -7,4 +9,4 @@ __version__ = '0.2.2' __url__ = 'https://github.com/p1c2u/openapi-core' __license__ = 'BSD 3-Clause License' -__all__ = ['create_spec', ] +__all__ = ['create_spec', 'validate_parameters', 'validate_body'] diff --git a/openapi_core/shortcuts.py b/openapi_core/shortcuts.py index 34164cc..0c976e5 100644 --- a/openapi_core/shortcuts.py +++ b/openapi_core/shortcuts.py @@ -3,7 +3,9 @@ from jsonschema.validators import RefResolver from openapi_spec_validator.validators import Dereferencer 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 def create_spec(spec_dict, spec_url=''): @@ -12,3 +14,25 @@ def create_spec(spec_dict, spec_url=''): dereferencer = Dereferencer(spec_resolver) spec_factory = SpecFactory(dereferencer) return spec_factory.create(spec_dict, spec_url=spec_url) + + +def validate_parameters(spec, request): + validator = RequestValidator(spec) + result = validator.validate(request) + try: + result.validate() + except OpenAPIBodyError: + return result.parameters + else: + return result.parameters + + +def validate_body(spec, request): + validator = RequestValidator(spec) + result = validator.validate(request) + try: + result.validate() + except OpenAPIParameterError: + return result.body + else: + return result.body diff --git a/openapi_core/wrappers.py b/openapi_core/wrappers.py index 21070b7..433c54b 100644 --- a/openapi_core/wrappers.py +++ b/openapi_core/wrappers.py @@ -3,8 +3,7 @@ import warnings from six.moves.urllib.parse import urljoin -from openapi_core.exceptions import OpenAPIParameterError, OpenAPIBodyError -from openapi_core.validators import RequestValidator +from openapi_core.shortcuts import validate_parameters, validate_body class BaseOpenAPIRequest(object): @@ -30,14 +29,7 @@ class BaseOpenAPIRequest(object): DeprecationWarning, ) # backward compatibility - validator = RequestValidator(spec) - result = validator.validate(self) - try: - result.validate() - except OpenAPIParameterError: - return result.body - else: - return result.body + return validate_body(spec, self) def get_parameters(self, spec): warnings.warn( @@ -46,14 +38,7 @@ class BaseOpenAPIRequest(object): DeprecationWarning, ) # backward compatibility - validator = RequestValidator(spec) - result = validator.validate(self) - try: - result.validate() - except OpenAPIBodyError: - return result.parameters - else: - return result.parameters + return validate_parameters(spec, self) class MockRequest(BaseOpenAPIRequest):