tortoise / tortoise/tortoise-orm

Better validation errors

Open
#1,460 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Is your feature request related to a problem? Please describe.
At the moment, validation errors are emitted with this code:

    for v in self.validators:
            if self.null and value is None:
                continue
            try:
                if isinstance(value, Enum):
                    v(value.value)
                else:
                    v(value)
            except ValidationError as exc:
                raise ValidationError(f"{self.model_field_name}: {exc}")

I usually want to get the value of the field that caused the error so that I can inform the frontend of the problematic field.

The problem is that, to get the value of the field that cause the error: self.model_field_name, I need to use a regular expression on the string.

Describe the solution you'd like
I would like to have an error class that has specific field and msg instance attributes. The msg attribute would not contain the field value.

Describe alternatives you've considered
Using a regex to isolate the field / msg.

Additional context
None.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in tortoise/fields/base.py at the validator loop linked in the issue, then search for ValidationError consumers and existing validation tests. The work is done when validation errors expose separate field and msg attributes, with the requested message behavior covered by tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.