Skip to content

Commit 3aaa3ce

Browse files
committed
Separate type and structure validation
1 parent 0915e94 commit 3aaa3ce

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

openapi_core/schema/schemas/models.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ class Schema(object):
3232
SchemaFormat.DATE.value: format_date,
3333
})
3434

35-
VALIDATOR_CALLABLE_GETTER = {
35+
TYPE_VALIDATOR_CALLABLE_GETTER = {
3636
None: lambda x: x,
3737
SchemaType.BOOLEAN: TypeValidator(bool),
3838
SchemaType.INTEGER: TypeValidator(integer_types, exclude=bool),
3939
SchemaType.NUMBER: TypeValidator(integer_types, float, exclude=bool),
4040
SchemaType.STRING: TypeValidator(binary_type, text_type),
4141
SchemaType.ARRAY: TypeValidator(list, tuple),
42-
SchemaType.OBJECT: AttributeValidator('__class__'),
42+
SchemaType.OBJECT: AttributeValidator('__dict__'),
4343
}
4444

4545
def __init__(
@@ -242,35 +242,35 @@ def _unmarshal_properties(self, value, one_of_schema=None):
242242
return properties
243243

244244
def get_validator_mapping(self):
245-
mapping = self.VALIDATOR_CALLABLE_GETTER.copy()
246-
mapping.update({
245+
mapping = {
247246
SchemaType.OBJECT: self._validate_object,
248-
})
247+
}
249248

250249
return defaultdict(lambda: lambda x: x, mapping)
251250

252251
def validate(self, value):
253252
if value is None:
254253
if not self.nullable:
255254
raise InvalidSchemaValue("Null value for non-nullable schema")
256-
return self.default
257-
258-
validator_mapping = self.get_validator_mapping()
259-
validator_callable = validator_mapping[self.type]
255+
return
260256

261-
if not validator_callable(value):
257+
# type validation
258+
type_validator_callable = self.TYPE_VALIDATOR_CALLABLE_GETTER[
259+
self.type]
260+
if not type_validator_callable(value):
262261
raise InvalidSchemaValue(
263262
"Value of {0} not valid type of {1}".format(
264263
value, self.type.value)
265264
)
266265

266+
# structure validation
267+
validator_mapping = self.get_validator_mapping()
268+
validator_callable = validator_mapping[self.type]
269+
validator_callable(value)
270+
267271
return value
268272

269273
def _validate_object(self, value):
270-
if not hasattr(value, '__dict__'):
271-
raise InvalidSchemaValue(
272-
"Value of {0} not an object".format(value))
273-
274274
properties = value.__dict__
275275

276276
if self.one_of:

0 commit comments

Comments
 (0)