66from six import iteritems
77
88from openapi_core .extensions .models .factories import ModelFactory
9- from openapi_core .schema .schemas .enums import SchemaType
9+ from openapi_core .schema .schemas .enums import SchemaFormat , SchemaType
1010from openapi_core .schema .schemas .exceptions import (
1111 InvalidSchemaValue , UndefinedSchemaProperty , MissingSchemaProperty ,
1212 OpenAPISchemaError , NoOneOfSchema , MultipleOneOfSchema ,
1313)
14- from openapi_core .schema .schemas .util import forcebool
14+ from openapi_core .schema .schemas .util import forcebool , format_date
1515
1616log = logging .getLogger (__name__ )
1717
@@ -25,6 +25,10 @@ class Schema(object):
2525 SchemaType .BOOLEAN : forcebool ,
2626 }
2727
28+ FORMAT_CALLABLE_GETTER = defaultdict (lambda : lambda x : x , {
29+ SchemaFormat .DATE .value : format_date ,
30+ })
31+
2832 def __init__ (
2933 self , schema_type = None , model = None , properties = None , items = None ,
3034 schema_format = None , required = None , default = None , nullable = False ,
@@ -92,6 +96,7 @@ def get_all_required_properties_names(self):
9296 def get_cast_mapping (self ):
9397 mapping = self .DEFAULT_CAST_CALLABLE_GETTER .copy ()
9498 mapping .update ({
99+ SchemaType .STRING : self ._unmarshal_string ,
95100 SchemaType .ARRAY : self ._unmarshal_collection ,
96101 SchemaType .OBJECT : self ._unmarshal_object ,
97102 })
@@ -110,7 +115,7 @@ def cast(self, value):
110115
111116 cast_mapping = self .get_cast_mapping ()
112117
113- if self .type in cast_mapping and value == '' :
118+ if self .type is not SchemaType . STRING and value == '' :
114119 return None
115120
116121 cast_callable = cast_mapping [self .type ]
@@ -139,6 +144,16 @@ def unmarshal(self, value):
139144
140145 return casted
141146
147+ def _unmarshal_string (self , value ):
148+ formatter = self .FORMAT_CALLABLE_GETTER [self .format ]
149+ try :
150+ return formatter (value )
151+ except ValueError :
152+ raise InvalidSchemaValue (
153+ "Failed to format value of {0} to {1}" .format (
154+ value , self .format )
155+ )
156+
142157 def _unmarshal_collection (self , value ):
143158 return list (map (self .items .unmarshal , value ))
144159
0 commit comments