Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#24008 closed Bug (fixed)

ValidationError crashes if initialised with a list that includes a ValidationError instance initialised with a dict.

Reported by: Sean Owned by: Andrey Maslov
Component: Core (Other) Version: 1.7
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

According to the documentation, ValidationError can be initialised with a list of ValidationError instances. However, if the list includes a ValidationError that was initialised with a dictionary then it crashes with the following error: 'ValidationError' object has no attribute 'error_list'.

This bug would occur if someone wanted to raise a ValidationError with a set of field errors together with one or more non-field errors for example.

For example:

>>> from django.core.exceptions import ValidationError
>>> ValidationError([ValidationError({'age', 'age cannot be negative'}), ValidationError('Some other error')])

The problem is caused because when a ValidationError instance is initialised with a dictionary, its error_list attribute is not set (instead error_dict is set) but a ValidationError that is initialised with a list tries to compile a flattened list of all errors using this attribute from each list element. Either ValidationError should be modified to accept a mixture of field and non-field errors (appears to me to be the best solution) or the documentation and error message should be changed to make usage clear.

Related to https://code.djangoproject.com/ticket/23976

Change History (6)

comment:1 by Tim Graham, 9 years ago

Component: UncategorizedCore (Other)
Description: modified (diff)
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug
Version: 1.61.7

comment:2 by Andrey Maslov, 9 years ago

Owner: changed from nobody to Andrey Maslov
Status: newassigned
Last edited 9 years ago by Andrey Maslov (previous) (diff)

comment:3 by Tim Graham, 9 years ago

Has patch: set
Patch needs improvement: set

comment:4 by Tim Graham, 9 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:5 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 7a878ca5cb50ad65fc465cb263a44cc93629f75c:

Fixed #24008 -- Fixed ValidationError crash with list of dicts.

comment:6 by Tim Graham <timograham@…>, 9 years ago

In 8de2a44064ca11749579a5887f59821fa35e1fdc:

[1.7.x] Fixed #24008 -- Fixed ValidationError crash with list of dicts.

Backport of 7a878ca5cb50ad65fc465cb263a44cc93629f75c from master

Note: See TracTickets for help on using tickets.
Back to Top