mirror of
https://github.com/correl/openapi-core.git
synced 2024-12-29 11:09:25 +00:00
Validate body and parameters shortcuts
This commit is contained in:
parent
37f09d7571
commit
624bf3de95
3 changed files with 31 additions and 20 deletions
|
@ -1,5 +1,7 @@
|
||||||
"""OpenAPI core module"""
|
"""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'
|
__author__ = 'Artur Maciąg'
|
||||||
__email__ = 'maciag.artur@gmail.com'
|
__email__ = 'maciag.artur@gmail.com'
|
||||||
|
@ -7,4 +9,4 @@ __version__ = '0.2.2'
|
||||||
__url__ = 'https://github.com/p1c2u/openapi-core'
|
__url__ = 'https://github.com/p1c2u/openapi-core'
|
||||||
__license__ = 'BSD 3-Clause License'
|
__license__ = 'BSD 3-Clause License'
|
||||||
|
|
||||||
__all__ = ['create_spec', ]
|
__all__ = ['create_spec', 'validate_parameters', 'validate_body']
|
||||||
|
|
|
@ -3,7 +3,9 @@ from jsonschema.validators import RefResolver
|
||||||
from openapi_spec_validator.validators import Dereferencer
|
from openapi_spec_validator.validators import Dereferencer
|
||||||
from openapi_spec_validator import default_handlers
|
from openapi_spec_validator import default_handlers
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
def create_spec(spec_dict, spec_url=''):
|
def create_spec(spec_dict, spec_url=''):
|
||||||
|
@ -12,3 +14,25 @@ def create_spec(spec_dict, spec_url=''):
|
||||||
dereferencer = Dereferencer(spec_resolver)
|
dereferencer = Dereferencer(spec_resolver)
|
||||||
spec_factory = SpecFactory(dereferencer)
|
spec_factory = SpecFactory(dereferencer)
|
||||||
return spec_factory.create(spec_dict, spec_url=spec_url)
|
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
|
||||||
|
|
|
@ -3,8 +3,7 @@ import warnings
|
||||||
|
|
||||||
from six.moves.urllib.parse import urljoin
|
from six.moves.urllib.parse import urljoin
|
||||||
|
|
||||||
from openapi_core.exceptions import OpenAPIParameterError, OpenAPIBodyError
|
from openapi_core.shortcuts import validate_parameters, validate_body
|
||||||
from openapi_core.validators import RequestValidator
|
|
||||||
|
|
||||||
|
|
||||||
class BaseOpenAPIRequest(object):
|
class BaseOpenAPIRequest(object):
|
||||||
|
@ -30,14 +29,7 @@ class BaseOpenAPIRequest(object):
|
||||||
DeprecationWarning,
|
DeprecationWarning,
|
||||||
)
|
)
|
||||||
# backward compatibility
|
# backward compatibility
|
||||||
validator = RequestValidator(spec)
|
return validate_body(spec, self)
|
||||||
result = validator.validate(self)
|
|
||||||
try:
|
|
||||||
result.validate()
|
|
||||||
except OpenAPIParameterError:
|
|
||||||
return result.body
|
|
||||||
else:
|
|
||||||
return result.body
|
|
||||||
|
|
||||||
def get_parameters(self, spec):
|
def get_parameters(self, spec):
|
||||||
warnings.warn(
|
warnings.warn(
|
||||||
|
@ -46,14 +38,7 @@ class BaseOpenAPIRequest(object):
|
||||||
DeprecationWarning,
|
DeprecationWarning,
|
||||||
)
|
)
|
||||||
# backward compatibility
|
# backward compatibility
|
||||||
validator = RequestValidator(spec)
|
return validate_parameters(spec, self)
|
||||||
result = validator.validate(self)
|
|
||||||
try:
|
|
||||||
result.validate()
|
|
||||||
except OpenAPIBodyError:
|
|
||||||
return result.parameters
|
|
||||||
else:
|
|
||||||
return result.parameters
|
|
||||||
|
|
||||||
|
|
||||||
class MockRequest(BaseOpenAPIRequest):
|
class MockRequest(BaseOpenAPIRequest):
|
||||||
|
|
Loading…
Reference in a new issue