Skip to content

Commit

Permalink
Merge pull request #42 from hardcoretech/fix/dj-err-json-serialization
Browse files Browse the repository at this point in the history
fix: DSVError are not JSON serializable
  • Loading branch information
CJHwong authored Jun 14, 2024
2 parents fd20a7a + 4852b36 commit 35fe68e
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

3.4.1
-----
- Fix `dsv` decorator to work with `DSVError`.
- Fix dsv_feature `spec_name` should not show empty value.


3.4.0
-----

Expand Down
2 changes: 1 addition & 1 deletion data_spec_validator/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '3.4.0'
__version__ = '3.4.1'
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 35fe68e

Please sign in to comment.