Validation schema errors iter fix

This commit is contained in:
p1c2u 2019-10-22 22:01:17 +01:00
parent 57cc70a9f0
commit 44de9c4322
3 changed files with 14 additions and 6 deletions

View file

@ -48,13 +48,19 @@ class UnmarshalValueError(UnmarshalError):
class InvalidSchemaValue(ValidateError):
value = attr.ib()
type = attr.ib()
schema_errors = attr.ib()
_schema_errors = attr.ib(default=None)
_schema_errors_iter = attr.ib(factory=list)
@property
def schema_errors(self):
if self._schema_errors is None:
self._schema_errors = list(self._schema_errors_iter)
return self._schema_errors
def __str__(self):
errors = list(self.schema_errors)
return (
"Value {value} not valid for schema of type {type}: {errors}"
).format(value=self.value, type=self.type, errors=errors)
).format(value=self.value, type=self.type, errors=self.schema_errors)
class UnmarshallerError(UnmarshalError):

View file

@ -194,7 +194,8 @@ class Schema(object):
return validator.validate(value)
except ValidationError:
errors_iter = validator.iter_errors(value)
raise InvalidSchemaValue(value, self.type, errors_iter)
raise InvalidSchemaValue(
value, self.type, schema_errors_iter=errors_iter)
def unmarshal(self, value, custom_formatters=None, strict=True):
"""Unmarshal parameter from the value."""

View file

@ -175,13 +175,14 @@ class TestPetstore(object):
response_result = response_validator.validate(request, response)
errors = response_result.errors[0].original_exception.schema_errors
original_exc = response_result.errors[0].original_exception
assert response_result.errors == [
InvalidMediaTypeValue(
original_exception=InvalidSchemaValue(
type=SchemaType.OBJECT,
value=data_json,
schema_errors=errors,
schema_errors=original_exc.schema_errors,
schema_errors_iter=original_exc._schema_errors_iter,
),
),
]