Skip to content

Commit

Permalink
fix: DSVError are not JSON serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
CJHwong committed Jun 13, 2024
1 parent fd20a7a commit 016acdc
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion data_spec_validator/decorator/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def _get_error_response(error, use_drf):
Return the error response based on the error type.
If the attribute use_drf is True, Raise DRF's exception to let DRF's exception handler do something about it.
"""
error_msg = {'messages': error.message}
error_msg = {'messages': [str(err) for err in error.message]}

if use_drf:
err_map = {
Expand Down
4 changes: 3 additions & 1 deletion data_spec_validator/spec/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ def wrap(cls: Type) -> Type:


def get_spec_name(spec) -> str:
return getattr(spec, _FEAT_PARAMS).spec_name if hasattr(spec, _FEAT_PARAMS) else spec.__name__
if hasattr(spec, _FEAT_PARAMS) and getattr(spec, _FEAT_PARAMS).spec_name:
return getattr(spec, _FEAT_PARAMS).spec_name
return spec.__name__


def get_err_mode(spec) -> ErrorMode:
Expand Down
3 changes: 2 additions & 1 deletion test/test_decorator_dj.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from data_spec_validator.decorator import dsv, dsv_request_meta
from data_spec_validator.decorator.decorators import ParseError
from data_spec_validator.spec import DIGIT_STR, LIST_OF, ONE_OF, STR, Checker, dsv_feature
from data_spec_validator.spec import DIGIT_STR, LIST_OF, ONE_OF, STR, Checker, ErrorMode, dsv_feature

from .utils import is_django_installed, make_request

Expand Down Expand Up @@ -223,6 +223,7 @@ def decorated_func(self, request, field_a):

def test_json_response_content(self):
# arrange
@dsv_feature(err_mode=ErrorMode.ALL)
class _ViewSpec:
named_arg = Checker([DIGIT_STR])

Expand Down
3 changes: 2 additions & 1 deletion test/test_decorator_drf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from parameterized import parameterized

from data_spec_validator.decorator import dsv, dsv_request_meta
from data_spec_validator.spec import DIGIT_STR, LIST_OF, ONE_OF, STR, Checker, dsv_feature
from data_spec_validator.spec import DIGIT_STR, LIST_OF, ONE_OF, STR, Checker, ErrorMode, dsv_feature

from .utils import is_drf_installed, make_request

Expand Down Expand Up @@ -178,6 +178,7 @@ def decorated_func(self, request, field_a):

def test_json_response_content(self):
# arrange
@dsv_feature(err_mode=ErrorMode.ALL)
class _ViewSpec:
field_a = Checker([DIGIT_STR])

Expand Down

0 comments on commit 016acdc

Please sign in to comment.