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

42 lines
1 KiB
Python
Raw Permalink Normal View History

2021-04-27 21:39:28 +00:00
from __future__ import division
2020-02-03 10:59:27 +00:00
from openapi_core.casting.schemas.exceptions import CastError
2021-04-27 21:16:30 +00:00
from openapi_core.types import NoValue
2020-01-23 22:48:34 +00:00
class PrimitiveCaster(object):
2020-02-03 13:41:26 +00:00
def __init__(self, schema, caster_callable):
self.schema = schema
2020-01-23 22:48:34 +00:00
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)
2020-02-03 13:41:26 +00:00
except (ValueError, TypeError):
2021-04-23 11:36:27 +00:00
raise CastError(value, self.schema['type'])
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):
2021-04-23 11:36:27 +00:00
return self.casters_factory.create(self.schema / 'items')
2020-01-23 22:48:34 +00:00
def __call__(self, value):
if value in (None, NoValue):
return value
return list(map(self.items_caster, value))