15
15
from openapi_core .schema .schemas ._format import oas30_format_checker
16
16
from openapi_core .schema .schemas .enums import SchemaFormat , SchemaType
17
17
from 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 ,
22
20
)
23
21
from openapi_core .schema .schemas .util import (
24
22
forcebool , format_date , format_datetime , format_byte , format_uuid ,
@@ -141,13 +139,6 @@ def get_all_required_properties_names(self):
141
139
142
140
return set (required )
143
141
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
-
151
142
def get_cast_mapping (self ):
152
143
mapping = self .TYPE_CAST_CALLABLE_GETTER .copy ()
153
144
mapping .update ({
@@ -167,8 +158,7 @@ def cast(self, value):
167
158
try :
168
159
return cast_callable (value )
169
160
except ValueError :
170
- raise InvalidSchemaValue (
171
- "Failed to cast value {value} to type {type}" , value , self .type )
161
+ raise CastError (value , self .type )
172
162
173
163
def _cast_collection (self , value ):
174
164
return list (map (self .items .cast , value ))
@@ -203,21 +193,21 @@ def validate(self, value, resolver=None):
203
193
try :
204
194
return validator .validate (value )
205
195
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 )
208
198
209
199
def unmarshal (self , value , custom_formatters = None , strict = True ):
210
200
"""Unmarshal parameter from the value."""
211
201
if self .deprecated :
212
202
warnings .warn ("The schema is deprecated" , DeprecationWarning )
213
203
if value is None :
214
204
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 )
216
207
return self .default
217
208
218
209
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 ))
221
211
222
212
unmarshal_mapping = self .get_unmarshal_mapping (
223
213
custom_formatters = custom_formatters , strict = strict )
@@ -228,12 +218,8 @@ def unmarshal(self, value, custom_formatters=None, strict=True):
228
218
unmarshal_callable = unmarshal_mapping [self .type ]
229
219
try :
230
220
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 )
237
223
238
224
return unmarshalled
239
225
@@ -268,7 +254,7 @@ def _unmarshal_any(self, value, custom_formatters=None, strict=True):
268
254
for subschema in self .one_of :
269
255
try :
270
256
unmarshalled = subschema .unmarshal (value , custom_formatters )
271
- except ( OpenAPISchemaError , TypeError , ValueError ) :
257
+ except UnmarshalError :
272
258
continue
273
259
else :
274
260
if result is not None :
@@ -285,17 +271,15 @@ def _unmarshal_any(self, value, custom_formatters=None, strict=True):
285
271
unmarshal_callable = unmarshal_mapping [schema_type ]
286
272
try :
287
273
return unmarshal_callable (value )
288
- except UnmarshallerStrictTypeError :
289
- continue
290
- except (OpenAPISchemaError , TypeError ):
274
+ except (UnmarshalError , ValueError ):
291
275
continue
292
276
293
277
log .warning ("failed to unmarshal any type" )
294
278
return value
295
279
296
280
def _unmarshal_collection (self , value , custom_formatters = None , strict = True ):
297
281
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 ) )
299
283
300
284
f = functools .partial (
301
285
self .items .unmarshal ,
@@ -306,7 +290,7 @@ def _unmarshal_collection(self, value, custom_formatters=None, strict=True):
306
290
def _unmarshal_object (self , value , model_factory = None ,
307
291
custom_formatters = None , strict = True ):
308
292
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 ) )
310
294
311
295
model_factory = model_factory or ModelFactory ()
312
296
@@ -316,7 +300,7 @@ def _unmarshal_object(self, value, model_factory=None,
316
300
try :
317
301
unmarshalled = self ._unmarshal_properties (
318
302
value , one_of_schema , custom_formatters = custom_formatters )
319
- except OpenAPISchemaError :
303
+ except ( UnmarshalError , ValueError ) :
320
304
pass
321
305
else :
322
306
if properties is not None :
@@ -348,10 +332,6 @@ def _unmarshal_properties(self, value, one_of_schema=None,
348
332
349
333
value_props_names = value .keys ()
350
334
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 )
355
335
356
336
properties = {}
357
337
if self .additional_properties is not True :
@@ -364,8 +344,6 @@ def _unmarshal_properties(self, value, one_of_schema=None,
364
344
try :
365
345
prop_value = value [prop_name ]
366
346
except KeyError :
367
- if prop_name in all_req_props_names :
368
- raise MissingSchemaProperty (prop_name )
369
347
if not prop .nullable and not prop .default :
370
348
continue
371
349
prop_value = prop .default
0 commit comments