Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#31306 closed New feature (wontfix)

Raise ValidationError on first failure.

Reported by: Doug Fultz Owned by: nobody
Component: Database layer (models, ORM) Version: 3.0
Severity: Normal Keywords: feature request validators
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently field validators are executed in order and any exceptions are collected before raising a single ValidationError for that field.
https://github.com/django/django/blob/master/django/db/models/fields/__init__.py#L600

I have identified a use case where a validator may reach our to an external service or be very expensive to run, compared to other validators on the field. It would help increase performance and decrease load on other services if there was an option for field validation to fail on the first exception and prevent subsequent validators from being executed.

An example might be a validator that looks like

...
validators = [RegexValidator(r'\w'), ExpensiveExists]

Where the regex validator is fast and runs locally while ExpensiveExists calls out to an external service. In this case, we would want validation to fail on the regex and not call the expensive validation.

Change History (2)

comment:1 by Mariusz Felisiak, 4 years ago

Resolution: wontfix
Status: newclosed
Summary: Raise ValidationError on first failureRaise ValidationError on first failure.

Thanks for this ticket, however you use case is quite niche, IMO. You can always use a custom field and override run_validators().

comment:2 by Adam Johnson, 4 years ago

It's also bad user experience to hide an error until others have been fixed. Django's run-them-all behaviour is the most user friendly.

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