2020-02-21 16:08:13 +00:00
|
|
|
"""OpenAPI core validation validators module"""
|
2021-04-27 21:39:28 +00:00
|
|
|
from __future__ import division
|
|
|
|
|
2021-02-13 01:10:16 +00:00
|
|
|
from openapi_core.unmarshalling.schemas.util import build_format_checker
|
2020-02-21 16:08:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BaseValidator(object):
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, spec,
|
2020-02-21 16:33:45 +00:00
|
|
|
base_url=None,
|
2020-02-21 16:08:13 +00:00
|
|
|
custom_formatters=None, custom_media_type_deserializers=None,
|
|
|
|
):
|
|
|
|
self.spec = spec
|
2020-02-21 16:33:45 +00:00
|
|
|
self.base_url = base_url
|
2021-02-13 01:10:16 +00:00
|
|
|
self.custom_formatters = custom_formatters or {}
|
2020-02-21 16:08:13 +00:00
|
|
|
self.custom_media_type_deserializers = custom_media_type_deserializers
|
|
|
|
|
2021-02-13 01:10:16 +00:00
|
|
|
self.format_checker = build_format_checker(**self.custom_formatters)
|
|
|
|
|
2020-02-21 16:08:13 +00:00
|
|
|
def _find_path(self, request):
|
2020-02-21 16:25:10 +00:00
|
|
|
from openapi_core.templating.paths.finders import PathFinder
|
2020-02-21 16:33:45 +00:00
|
|
|
finder = PathFinder(self.spec, base_url=self.base_url)
|
2020-02-21 16:25:10 +00:00
|
|
|
return finder.find(request)
|
2020-02-21 16:08:13 +00:00
|
|
|
|
2021-03-30 12:09:39 +00:00
|
|
|
def _get_media_type(self, content, request_or_response):
|
|
|
|
from openapi_core.templating.media_types.finders import MediaTypeFinder
|
|
|
|
finder = MediaTypeFinder(content)
|
|
|
|
return finder.find(request_or_response)
|
|
|
|
|
2021-04-23 11:36:27 +00:00
|
|
|
def _deserialise_data(self, mimetype, value):
|
2020-02-21 16:08:13 +00:00
|
|
|
from openapi_core.deserializing.media_types.factories import (
|
|
|
|
MediaTypeDeserializersFactory,
|
|
|
|
)
|
|
|
|
deserializers_factory = MediaTypeDeserializersFactory(
|
|
|
|
self.custom_media_type_deserializers)
|
2021-04-23 11:36:27 +00:00
|
|
|
deserializer = deserializers_factory.create(mimetype)
|
2020-02-21 16:08:13 +00:00
|
|
|
return deserializer(value)
|
|
|
|
|
|
|
|
def _cast(self, param_or_media_type, value):
|
|
|
|
# return param_or_media_type.cast(value)
|
2021-04-23 11:36:27 +00:00
|
|
|
if not 'schema' in param_or_media_type:
|
2020-02-21 16:08:13 +00:00
|
|
|
return value
|
|
|
|
|
|
|
|
from openapi_core.casting.schemas.factories import SchemaCastersFactory
|
|
|
|
casters_factory = SchemaCastersFactory()
|
2021-04-23 11:36:27 +00:00
|
|
|
schema = param_or_media_type / 'schema'
|
|
|
|
caster = casters_factory.create(schema)
|
2020-02-21 16:08:13 +00:00
|
|
|
return caster(value)
|
|
|
|
|
|
|
|
def _unmarshal(self, param_or_media_type, value, context):
|
2021-04-23 11:36:27 +00:00
|
|
|
if not 'schema' in param_or_media_type:
|
2020-02-21 16:08:13 +00:00
|
|
|
return value
|
|
|
|
|
|
|
|
from openapi_core.unmarshalling.schemas.factories import (
|
|
|
|
SchemaUnmarshallersFactory,
|
|
|
|
)
|
2021-04-23 11:36:27 +00:00
|
|
|
spec_resolver = self.spec.accessor.dereferencer.resolver_manager.resolver
|
2020-02-21 16:08:13 +00:00
|
|
|
unmarshallers_factory = SchemaUnmarshallersFactory(
|
2021-04-23 11:36:27 +00:00
|
|
|
spec_resolver, self.format_checker,
|
2021-02-13 01:10:16 +00:00
|
|
|
self.custom_formatters, context=context,
|
2020-02-21 16:08:13 +00:00
|
|
|
)
|
2021-04-23 11:36:27 +00:00
|
|
|
schema = param_or_media_type / 'schema'
|
|
|
|
unmarshaller = unmarshallers_factory.create(schema)
|
2020-02-21 16:08:13 +00:00
|
|
|
return unmarshaller(value)
|