Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dump error is not raised in marshmallow processor #211

Open
inkhey opened this issue Jul 8, 2021 · 0 comments
Open

Dump error is not raised in marshmallow processor #211

inkhey opened this issue Jul 8, 2021 · 0 comments

Comments

@inkhey
Copy link
Contributor

inkhey commented Jul 8, 2021

Hapic raise not all error during the dump in the marshmallow processor.

Workaround with custom processor that is not perfect (does not "merge the dump and the load errors") :

import typing

from hapic import MarshmallowProcessor
from hapic.exception import OutputValidationException
from hapic.exception import ValidationException
from hapic.processor.main import ProcessValidationError
from hapic.processor.main import Processor


class MyProcessor(MarshmallowProcessor):
    """Patched hapic processor that return error when dump data is not correct"""

    def dump(self, data: typing.Any) -> typing.Any:
        """
        Use schema to validate given data and return dumped data.
        If validation fail, raise InputValidationException
        :param data: data to validate and dump
        :return: dumped data
        """
        clean_data = self.clean_data(data)
        dump = self.schema.dump(clean_data)
        dump_data = dump.data
        errors = dump.errors
        if not errors:
            # Re-validate with dumped data
            errors = self.schema.load(dump_data).errors
        if errors:
            raise ValidationException("Error when dumping: {}".format(str(errors)))

        return dump_data

    def dump_output(self, output_data: typing.Any) -> typing.Union[typing.Dict, typing.List]:
        """
        Dump output data and raise OutputValidationException if validation error
        :param output_data: output data to validate
        :return: given data
        """
        clean_data = self.clean_data(output_data)
        dump = self.schema.dump(clean_data)
        dump_data = dump.data
        errors = dump.errors
        if not errors:
            # Re-validate with dumped data
            errors = self.schema.load(dump_data).errors
        if errors:
            raise OutputValidationException("Error when validate input: {}".format(str(errors)))

        return dump_data

    def get_output_validation_error(self, data_to_validate: typing.Any) -> ProcessValidationError:
        """
        Return ProcessValidationError for given output data
        :param data_to_validate: output data to use
        :return: ProcessValidationError instance for given output data
        """
        clean_data = self.clean_data(data_to_validate)
        dump = self.schema.dump(clean_data)
        dump_data = dump.data
        errors = dump.errors
        if not errors:
            # Re-validate with dumped data
            errors = self.schema.load(dump_data).errors
        return ProcessValidationError(message="Validation error of output data", details=errors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant