tortoise / tortoise/tortoise-orm
Better validation errors
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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