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

Incorrect formatting of error when raising from post_load #242

Open
Cilyan opened this issue Aug 10, 2019 · 0 comments
Open

Incorrect formatting of error when raising from post_load #242

Cilyan opened this issue Aug 10, 2019 · 0 comments

Comments

@Cilyan
Copy link

Cilyan commented Aug 10, 2019

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.

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