openapi-core/openapi_core/casting/schemas/casters.py

39 lines
981 B
Python
Raw Normal View History

2020-02-03 10:59:27 +00:00
from openapi_core.casting.schemas.exceptions import CastError
2020-01-23 22:48:34 +00:00
from openapi_core.schema.schemas.types import NoValue
class PrimitiveCaster(object):
def __init__(self, caster_callable):
self.caster_callable = caster_callable
def __call__(self, value):
if value in (None, NoValue):
return value
2020-02-03 10:59:27 +00:00
try:
return self.caster_callable(value)
except (ValueError, TypeError) as exc:
raise CastError(exc, self.caster_callable)
2020-01-23 22:48:34 +00:00
class DummyCaster(object):
def __call__(self, value):
return value
class ArrayCaster(object):
def __init__(self, schema, casters_factory):
self.schema = schema
self.casters_factory = casters_factory
@property
def items_caster(self):
return self.casters_factory.create(self.schema.items)
def __call__(self, value):
if value in (None, NoValue):
return value
return list(map(self.items_caster, value))