Skip to content

Commit

Permalink
feat: add FieldValidationError
Browse files Browse the repository at this point in the history
  • Loading branch information
long2ice committed Aug 23, 2023
1 parent af9a532 commit ad1c69a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Changelog
------
Added
^^^^^
- Add field to ValidationError. (#1460)
- Add `FieldValidationError` to raise error when field validation error. (#1460)

0.20.0
------
Expand Down
25 changes: 17 additions & 8 deletions tortoise/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from typing import Optional
import typing

if typing.TYPE_CHECKING:
from tortoise.fields import Field


class BaseORMException(Exception):
Expand Down Expand Up @@ -79,15 +82,21 @@ class ValidationError(BaseORMException):
The ValidationError is raised when validators of field validate failed.
"""

def __init__(self, msg: str, field: Optional[str] = None):
self.field = field
self.msg = msg

def __str__(self):
return self.msg


class UnSupportedError(BaseORMException):
"""
The UnSupportedError is raised when operation is not supported.
"""


class FieldValidationError(ValidationError):
"""
The FieldValidationError is raised when validators of field validate failed.
"""

def __init__(self, field: Field, msg: str):
self.field = field
self.msg = msg

def __str__(self):
return self.msg
4 changes: 2 additions & 2 deletions tortoise/fields/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

from pypika.terms import Term

from tortoise.exceptions import ConfigurationError, ValidationError
from tortoise.exceptions import ConfigurationError, ValidationError, FieldValidationError
from tortoise.validators import Validator

if TYPE_CHECKING: # pragma: nocoverage
Expand Down Expand Up @@ -259,7 +259,7 @@ def validate(self, value: Any):
else:
v(value)
except ValidationError as exc:
raise ValidationError(f"{self.model_field_name}: {exc}", self.model_field_name)
raise FieldValidationError(self, f"{self.model_field_name}: {exc}") from exc

@property
def required(self) -> bool:
Expand Down

0 comments on commit ad1c69a

Please sign in to comment.