Skip to content

Commit 399602f

Browse files
committed
sync master changes with exception refactoring
1 parent 17ea740 commit 399602f

File tree

2 files changed

+24
-40
lines changed

2 files changed

+24
-40
lines changed

openapi_core/schema/schemas/models.py

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,8 @@ def _validate_collection(self, value, custom_formatters=None):
354354
)
355355
if len(value) < self.min_items:
356356
raise InvalidSchemaValue(
357-
"Value must contain at least {0} item(s),"
358-
" {1} found".format(
359-
self.min_items, len(value))
360-
)
357+
"Value must contain at least {type} item(s),"
358+
" {value} found", len(value), self.min_items)
361359
if self.max_items is not None:
362360
if self.max_items < 0:
363361
raise OpenAPISchemaError(
@@ -366,47 +364,36 @@ def _validate_collection(self, value, custom_formatters=None):
366364
)
367365
if len(value) > self.max_items:
368366
raise InvalidSchemaValue(
369-
"Value must contain at most {0} item(s),"
370-
" {1} found".format(
371-
self.max_items, len(value))
372-
)
367+
"Value must contain at most {value} item(s),"
368+
" {type} found", len(value), self.max_items)
373369
if self.unique_items and len(set(value)) != len(value):
374-
raise InvalidSchemaValue("Value may not contain duplicate items")
370+
raise OpenAPISchemaError("Value may not contain duplicate items")
375371

376372
f = functools.partial(self.items.validate,
377373
custom_formatters=custom_formatters)
378374
return list(map(f, value))
379375

380-
def _validate_number(self, value):
376+
def _validate_number(self, value, custom_formatters=None):
381377
if self.minimum is not None:
382378
if self.exclusive_minimum and value <= self.minimum:
383379
raise InvalidSchemaValue(
384-
"Value {0} is not less than or equal to {1}".format(
385-
value, self.minimum)
386-
)
380+
"Value {value} is not less than or equal to {type}", value, self.minimum)
387381
elif value < self.minimum:
388382
raise InvalidSchemaValue(
389-
"Value {0} is not less than {1}".format(
390-
value, self.minimum)
391-
)
383+
"Value {value} is not less than {type}", value, self.minimum)
392384

393385
if self.maximum is not None:
394386
if self.exclusive_maximum and value >= self.maximum:
395387
raise InvalidSchemaValue(
396-
"Value {0} is not greater than or equal to {1}".format(
397-
value, self.maximum)
398-
)
388+
"Value {value} is not greater than or equal to {type}", value, self.maximum)
399389
elif value > self.maximum:
400390
raise InvalidSchemaValue(
401-
"Value {0} is not greater than {1}".format(
402-
value, self.maximum)
403-
)
391+
"Value {value} is not greater than {type}", value, self.maximum)
404392

405393
if self.multiple_of is not None and value % self.multiple_of:
406394
raise InvalidSchemaValue(
407-
"Value {0} is not a multiple of {1}".format(
395+
"Value {value} is not a multiple of {type}",
408396
value, self.multiple_of)
409-
)
410397

411398
def _validate_string(self, value, custom_formatters=None):
412399
try:
@@ -435,8 +422,8 @@ def _validate_string(self, value, custom_formatters=None):
435422
)
436423
if len(value) < self.min_length:
437424
raise InvalidSchemaValue(
438-
"Value is shorter than the minimum length of {0}".format(
439-
self.min_length)
425+
"Value is shorter ({value}) than the minimum length of {type}",
426+
len(value), self.min_length
440427
)
441428
if self.max_length is not None:
442429
if self.max_length < 0:
@@ -446,13 +433,13 @@ def _validate_string(self, value, custom_formatters=None):
446433
)
447434
if len(value) > self.max_length:
448435
raise InvalidSchemaValue(
449-
"Value is longer than the maximum length of {0}".format(
450-
self.max_length)
436+
"Value is longer ({value}) than the maximum length of {type}",
437+
len(value), self.max_length
451438
)
452439
if self.pattern is not None and not self.pattern.search(value):
453440
raise InvalidSchemaValue(
454-
"Value {0} does not match the pattern {1}".format(
455-
value, self.pattern.pattern)
441+
"Value {value} does not match the pattern {type}",
442+
value, self.pattern.pattern
456443
)
457444

458445
return True
@@ -490,9 +477,8 @@ def _validate_object(self, value, custom_formatters=None):
490477

491478
if len(properties) < self.min_properties:
492479
raise InvalidSchemaValue(
493-
"Value must contain at least {0} properties,"
494-
" {1} found".format(
495-
self.min_properties, len(properties))
480+
"Value must contain at least {type} properties,"
481+
" {value} found", len(properties), self.min_properties
496482
)
497483

498484
if self.max_properties is not None:
@@ -503,9 +489,8 @@ def _validate_object(self, value, custom_formatters=None):
503489
)
504490
if len(properties) > self.max_properties:
505491
raise InvalidSchemaValue(
506-
"Value must contain at most {0} properties,"
507-
" {1} found".format(
508-
self.max_properties, len(properties))
492+
"Value must contain at most {type} properties,"
493+
" {value} found", len(properties), self.max_properties
509494
)
510495

511496
return True

tests/integration/test_petstore.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from openapi_core.schema.request_bodies.models import RequestBody
1717
from openapi_core.schema.responses.models import Response
1818
from openapi_core.schema.schemas.exceptions import (
19-
UndefinedSchemaProperty, MissingSchemaProperty, NoOneOfSchema,
2019
NoValidSchema,
2120
)
2221
from openapi_core.schema.schemas.models import Schema
@@ -615,7 +614,7 @@ def test_post_no_one_of_schema(self, spec, spec_dict):
615614
},
616615
}
617616

618-
with pytest.raises(NoOneOfSchema):
617+
with pytest.raises(InvalidMediaTypeValue):
619618
request.get_body(spec)
620619

621620
def test_post_cats_only_required_body(self, spec, spec_dict):
@@ -952,7 +951,7 @@ def test_post_tags_extra_body_properties(self, spec, spec_dict):
952951

953952
assert parameters == {}
954953

955-
with pytest.raises(UndefinedSchemaProperty):
954+
with pytest.raises(InvalidMediaTypeValue):
956955
request.get_body(spec)
957956

958957
def test_post_tags_empty_body(self, spec, spec_dict):
@@ -970,7 +969,7 @@ def test_post_tags_empty_body(self, spec, spec_dict):
970969

971970
assert parameters == {}
972971

973-
with pytest.raises(MissingSchemaProperty):
972+
with pytest.raises(InvalidMediaTypeValue):
974973
request.get_body(spec)
975974

976975
def test_post_tags_wrong_property_type(self, spec):

0 commit comments

Comments
 (0)