1515from openapi_core .schema .schemas ._format import oas30_format_checker
1616from openapi_core .schema .schemas .enums import SchemaFormat , SchemaType
1717from openapi_core .schema .schemas .exceptions import (
18- InvalidSchemaValue , UndefinedSchemaProperty , MissingSchemaProperty ,
19- OpenAPISchemaError , NoValidSchema ,
20- UndefinedItemsSchema , InvalidCustomFormatSchemaValue , InvalidSchemaProperty ,
21- UnmarshallerStrictTypeError ,
18+ CastError , InvalidSchemaValue ,
19+ UnmarshallerError , UnmarshalValueError , UnmarshalError ,
2220)
2321from openapi_core .schema .schemas .util import (
2422 forcebool , format_date , format_datetime , format_byte , format_uuid ,
@@ -141,13 +139,6 @@ def get_all_required_properties_names(self):
141139
142140 return set (required )
143141
144- def are_additional_properties_allowed (self , one_of_schema = None ):
145- return (
146- (self .additional_properties is not False ) and
147- (one_of_schema is None or
148- one_of_schema .additional_properties is not False )
149- )
150-
151142 def get_cast_mapping (self ):
152143 mapping = self .TYPE_CAST_CALLABLE_GETTER .copy ()
153144 mapping .update ({
@@ -167,8 +158,7 @@ def cast(self, value):
167158 try :
168159 return cast_callable (value )
169160 except ValueError :
170- raise InvalidSchemaValue (
171- "Failed to cast value {value} to type {type}" , value , self .type )
161+ raise CastError (value , self .type )
172162
173163 def _cast_collection (self , value ):
174164 return list (map (self .items .cast , value ))
@@ -203,21 +193,21 @@ def validate(self, value, resolver=None):
203193 try :
204194 return validator .validate (value )
205195 except ValidationError :
206- # TODO: pass validation errors
207- raise InvalidSchemaValue ("Value not valid for schema" , value , self .type )
196+ errors_iter = validator . iter_errors ( value )
197+ raise InvalidSchemaValue (value , self .type , errors_iter )
208198
209199 def unmarshal (self , value , custom_formatters = None , strict = True ):
210200 """Unmarshal parameter from the value."""
211201 if self .deprecated :
212202 warnings .warn ("The schema is deprecated" , DeprecationWarning )
213203 if value is None :
214204 if not self .nullable :
215- raise InvalidSchemaValue ("Null value for non-nullable schema" , value , self .type )
205+ raise UnmarshalError (
206+ "Null value for non-nullable schema" , value , self .type )
216207 return self .default
217208
218209 if self .enum and value not in self .enum :
219- raise InvalidSchemaValue (
220- "Value {value} not in enum choices: {type}" , value , self .enum )
210+ raise UnmarshalError ("Invalid value for enum: {0}" .format (value ))
221211
222212 unmarshal_mapping = self .get_unmarshal_mapping (
223213 custom_formatters = custom_formatters , strict = strict )
@@ -228,12 +218,8 @@ def unmarshal(self, value, custom_formatters=None, strict=True):
228218 unmarshal_callable = unmarshal_mapping [self .type ]
229219 try :
230220 unmarshalled = unmarshal_callable (value )
231- except UnmarshallerStrictTypeError :
232- raise InvalidSchemaValue (
233- "Value {value} is not of type {type}" , value , self .type )
234- except ValueError :
235- raise InvalidSchemaValue (
236- "Failed to unmarshal value {value} to type {type}" , value , self .type )
221+ except ValueError as exc :
222+ raise UnmarshalValueError (value , self .type , exc )
237223
238224 return unmarshalled
239225
@@ -268,7 +254,7 @@ def _unmarshal_any(self, value, custom_formatters=None, strict=True):
268254 for subschema in self .one_of :
269255 try :
270256 unmarshalled = subschema .unmarshal (value , custom_formatters )
271- except ( OpenAPISchemaError , TypeError , ValueError ) :
257+ except UnmarshalError :
272258 continue
273259 else :
274260 if result is not None :
@@ -285,17 +271,15 @@ def _unmarshal_any(self, value, custom_formatters=None, strict=True):
285271 unmarshal_callable = unmarshal_mapping [schema_type ]
286272 try :
287273 return unmarshal_callable (value )
288- except UnmarshallerStrictTypeError :
289- continue
290- except (OpenAPISchemaError , TypeError ):
274+ except (UnmarshalError , ValueError ):
291275 continue
292276
293277 log .warning ("failed to unmarshal any type" )
294278 return value
295279
296280 def _unmarshal_collection (self , value , custom_formatters = None , strict = True ):
297281 if not isinstance (value , (list , tuple )):
298- raise InvalidSchemaValue ( "Value { value} is not of type {type}" , value , self . type )
282+ raise ValueError ( "Invalid value for collection: {0}" . format ( value ) )
299283
300284 f = functools .partial (
301285 self .items .unmarshal ,
@@ -306,7 +290,7 @@ def _unmarshal_collection(self, value, custom_formatters=None, strict=True):
306290 def _unmarshal_object (self , value , model_factory = None ,
307291 custom_formatters = None , strict = True ):
308292 if not isinstance (value , (dict , )):
309- raise InvalidSchemaValue ( "Value { value} is not of type {type}" , value , self . type )
293+ raise ValueError ( "Invalid value for object: {0}" . format ( value ) )
310294
311295 model_factory = model_factory or ModelFactory ()
312296
@@ -316,7 +300,7 @@ def _unmarshal_object(self, value, model_factory=None,
316300 try :
317301 unmarshalled = self ._unmarshal_properties (
318302 value , one_of_schema , custom_formatters = custom_formatters )
319- except OpenAPISchemaError :
303+ except ( UnmarshalError , ValueError ) :
320304 pass
321305 else :
322306 if properties is not None :
@@ -348,10 +332,6 @@ def _unmarshal_properties(self, value, one_of_schema=None,
348332
349333 value_props_names = value .keys ()
350334 extra_props = set (value_props_names ) - set (all_props_names )
351- extra_props_allowed = self .are_additional_properties_allowed (
352- one_of_schema )
353- if extra_props and not extra_props_allowed :
354- raise UndefinedSchemaProperty (extra_props )
355335
356336 properties = {}
357337 if self .additional_properties is not True :
@@ -364,8 +344,6 @@ def _unmarshal_properties(self, value, one_of_schema=None,
364344 try :
365345 prop_value = value [prop_name ]
366346 except KeyError :
367- if prop_name in all_req_props_names :
368- raise MissingSchemaProperty (prop_name )
369347 if not prop .nullable and not prop .default :
370348 continue
371349 prop_value = prop .default
0 commit comments