marshmallow-jsonapi icon indicating copy to clipboard operation
marshmallow-jsonapi copied to clipboard

Incorrect formatting of error when raising from post_load

Open Cilyan opened this issue 6 years ago • 0 comments

I moticed that the error is incorrectly formatted when one raises a ValidationError from a pre/post decorator.

For example I have this in my Schema:

@post_load
def merge_dates(self, data):
    # Do manipulation
    # ...
    # Additional validation on reconstructed fields
    if start > end:
        raise ValidationError("Start date and time must precede end date and time")

When an error occurs during usual validation, the errors is a dictionary with details:

>>> data = {"data": {"type": "timeslot", "attributes": {"start_date": "10/08/2019","start_time": "20:AA","end_date": "10/08/2019","end_time": "19:30"}}}
>>> sc.load(data)
UnmarshalResult(
    data={'end_date': datetime.date(2019, 10, 8), 'start_date': datetime.date(2019, 10, 8), 'end_time': datetime.time(19, 30)},
    errors={'errors': [{'detail': 'Not a valid time.', 'source': {'pointer': '/data/attributes/start_time'}}]}
)

But when I input some data that hits the custom validation, then the error is a string:

>>> data = {"data": {"type": "timeslot", "attributes": {"start_date": "10/08/2019","start_time": "20:00","end_date": "10/08/2019","end_time": "19:30"}}}
>>> sc.load(data)
UnmarshalResult(
    data={'start_time': datetime.time(20, 0), 'end_date': datetime.date(2019, 10, 8), 'start_date': datetime.date(2019, 10, 8), 'end_time': datetime.time(19, 30)},
    errors={'errors': ['Start date and time must precede end date and time']}
)

That behavior make it fail when used behind flask-rest-jsonapi.

For the moment, the workaround I found, by deep analysis of the code, is to raise a dictionary instead using the failing attribute as key and a list of string as value, so that the for loop doesn't explode the error string.

raise ValidationError({'start': ["Start date and time must precede end date and time"]})

If that's the way to go, I did not find this trick in the documentation.

Cilyan avatar Aug 10 '19 14:08 Cilyan