diff --git a/openapi_core/schema/schemas/exceptions.py b/openapi_core/schema/schemas/exceptions.py index 2b87923..f325d18 100644 --- a/openapi_core/schema/schemas/exceptions.py +++ b/openapi_core/schema/schemas/exceptions.py @@ -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): diff --git a/openapi_core/schema/schemas/models.py b/openapi_core/schema/schemas/models.py index 534c7b7..6869cc7 100644 --- a/openapi_core/schema/schemas/models.py +++ b/openapi_core/schema/schemas/models.py @@ -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.""" diff --git a/tests/integration/validation/test_petstore.py b/tests/integration/validation/test_petstore.py index 38ab8ba..f2ecaee 100644 --- a/tests/integration/validation/test_petstore.py +++ b/tests/integration/validation/test_petstore.py @@ -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, ), ), ]