Skip to content

Commit 8845982

Browse files
committed
Schema validate exceptions refactor
1 parent 939cec9 commit 8845982

File tree

4 files changed

+25
-71
lines changed

4 files changed

+25
-71
lines changed

openapi_core/schema/schemas/exceptions.py

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,24 @@ class OpenAPISchemaError(OpenAPIMappingError):
77
pass
88

99

10+
@attr.s(hash=True)
11+
class CastError(OpenAPISchemaError):
12+
"""Schema cast operation error"""
13+
value = attr.ib()
14+
type = attr.ib()
15+
16+
def __str__(self):
17+
return "Failed to cast value {value} to type {type}".format(
18+
value=self.value, type=self.type)
19+
20+
21+
class ValidateError(OpenAPISchemaError):
22+
"""Schema validate operation error"""
23+
pass
24+
25+
1026
class UnmarshallError(OpenAPISchemaError):
11-
"""Unmarshall operation error"""
27+
"""Schema unmarshall operation error"""
1228
pass
1329

1430

@@ -29,56 +45,13 @@ def __str__(self):
2945

3046

3147
@attr.s(hash=True)
32-
class NoValidSchema(OpenAPISchemaError):
33-
value = attr.ib()
34-
35-
def __str__(self):
36-
return "No valid schema found for value: {0}".format(self.value)
37-
38-
39-
@attr.s(hash=True)
40-
class UndefinedItemsSchema(OpenAPISchemaError):
41-
type = attr.ib()
42-
43-
def __str__(self):
44-
return "Null value for schema type {0}".format(self.type)
45-
46-
47-
@attr.s(hash=True)
48-
class InvalidSchemaValue(OpenAPISchemaError):
49-
msg = attr.ib()
48+
class InvalidSchemaValue(ValidateError):
5049
value = attr.ib()
5150
type = attr.ib()
5251

5352
def __str__(self):
54-
return self.msg.format(value=self.value, type=self.type)
55-
56-
57-
@attr.s(hash=True)
58-
class UndefinedSchemaProperty(OpenAPISchemaError):
59-
extra_props = attr.ib()
60-
61-
def __str__(self):
62-
return "Extra unexpected properties found in schema: {0}".format(
63-
self.extra_props)
64-
65-
66-
@attr.s(hash=True)
67-
class InvalidSchemaProperty(OpenAPISchemaError):
68-
property_name = attr.ib()
69-
original_exception = attr.ib()
70-
71-
def __str__(self):
72-
return "Invalid schema property {0}: {1}".format(
73-
self.property_name, self.original_exception)
74-
75-
76-
@attr.s(hash=True)
77-
class MissingSchemaProperty(OpenAPISchemaError):
78-
property_name = attr.ib()
79-
80-
def __str__(self):
81-
return "Missing schema property: {0}".format(self.property_name)
53+
return "Value {value} not valid for schema of type {type}".format(
54+
value=self.value, type=self.type)
8255

8356

8457
class UnmarshallerError(UnmarshallError):

openapi_core/schema/schemas/models.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
from openapi_core.schema.schemas._format import oas30_format_checker
1616
from openapi_core.schema.schemas.enums import SchemaFormat, SchemaType
1717
from openapi_core.schema.schemas.exceptions import (
18-
InvalidSchemaValue, UndefinedSchemaProperty, MissingSchemaProperty,
19-
OpenAPISchemaError, NoValidSchema,
20-
UndefinedItemsSchema, InvalidSchemaProperty,
18+
CastError, InvalidSchemaValue,
2119
UnmarshallerError, UnmarshallValueError, UnmarshallError,
2220
)
2321
from openapi_core.schema.schemas.util import (
@@ -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))
@@ -204,7 +194,7 @@ def validate(self, value, resolver=None):
204194
return validator.validate(value)
205195
except ValidationError:
206196
# TODO: pass validation errors
207-
raise InvalidSchemaValue("Value not valid for schema", value, self.type)
197+
raise InvalidSchemaValue(value, self.type)
208198

209199
def unmarshal(self, value, custom_formatters=None, strict=True):
210200
"""Unmarshal parameter from the value."""
@@ -310,7 +300,7 @@ def _unmarshal_object(self, value, model_factory=None,
310300
try:
311301
unmarshalled = self._unmarshal_properties(
312302
value, one_of_schema, custom_formatters=custom_formatters)
313-
except OpenAPISchemaError:
303+
except (UnmarshallError, ValueError):
314304
pass
315305
else:
316306
if properties is not None:
@@ -342,10 +332,6 @@ def _unmarshal_properties(self, value, one_of_schema=None,
342332

343333
value_props_names = value.keys()
344334
extra_props = set(value_props_names) - set(all_props_names)
345-
extra_props_allowed = self.are_additional_properties_allowed(
346-
one_of_schema)
347-
if extra_props and not extra_props_allowed:
348-
raise UndefinedSchemaProperty(extra_props)
349335

350336
properties = {}
351337
if self.additional_properties is not True:
@@ -358,8 +344,6 @@ def _unmarshal_properties(self, value, one_of_schema=None,
358344
try:
359345
prop_value = value[prop_name]
360346
except KeyError:
361-
if prop_name in all_req_props_names:
362-
raise MissingSchemaProperty(prop_name)
363347
if not prop.nullable and not prop.default:
364348
continue
365349
prop_value = prop.default

openapi_core/schema/schemas/unmarshallers.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
from openapi_core.schema.schemas.enums import SchemaFormat, SchemaType
44
from openapi_core.schema.schemas.exceptions import (
5-
InvalidSchemaValue, InvalidCustomFormatSchemaValue,
6-
OpenAPISchemaError,
7-
InvalidSchemaProperty,
5+
InvalidCustomFormatSchemaValue,
86
UnmarshallerStrictTypeError,
97
FormatterNotFoundError,
108
)

tests/integration/validation/test_petstore.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ def test_get_pets_invalid_response(self, spec, response_validator):
178178
assert response_result.errors == [
179179
InvalidMediaTypeValue(
180180
original_exception=InvalidSchemaValue(
181-
msg='Value not valid for schema',
182181
type=SchemaType.OBJECT,
183182
value=data_json,
184183
),

0 commit comments

Comments
 (0)