|
23 | 23 | class StrOrDictField(Field): |
24 | 24 | """URI or Dict field for Marshmallow.""" |
25 | 25 |
|
26 | | - def _serialize(self, value, attr, obj, **kwargs): |
27 | | - return value |
28 | | - |
29 | 26 | def _deserialize(self, value, attr, data, **kwargs): |
30 | | - if isinstance(value, (str, dict)): |
31 | | - return value |
32 | | - else: |
| 27 | + if not isinstance(value, (str, dict)): |
33 | 28 | raise ValidationError("Field should be str or dict") |
| 29 | + return super()._deserialize(value, attr, data, **kwargs) |
34 | 30 |
|
35 | 31 |
|
36 | 32 | class StrOrNumberField(Field): |
37 | 33 | """String or Number field for Marshmallow.""" |
38 | 34 |
|
39 | | - def _serialize(self, value, attr, obj, **kwargs): |
40 | | - return value |
41 | | - |
42 | 35 | def _deserialize(self, value, attr, data, **kwargs): |
43 | | - if isinstance(value, (str, float, int)): |
44 | | - return value |
45 | | - else: |
| 36 | + if not isinstance(value, (str, float, int)): |
46 | 37 | raise ValidationError("Field should be str or int or float") |
| 38 | + return super()._deserialize(value, attr, data, **kwargs) |
47 | 39 |
|
48 | 40 |
|
49 | 41 | class DictOrDictListField(Field): |
50 | 42 | """Dict or Dict List field for Marshmallow.""" |
51 | 43 |
|
52 | | - def _serialize(self, value, attr, obj, **kwargs): |
53 | | - return value |
54 | | - |
55 | 44 | def _deserialize(self, value, attr, data, **kwargs): |
56 | | - # dict |
57 | | - if isinstance(value, dict): |
58 | | - return value |
59 | | - # list of dicts |
60 | | - elif isinstance(value, list) and all(isinstance(item, dict) for item in value): |
61 | | - return value |
62 | | - else: |
63 | | - raise ValidationError("Field should be dict or list of dicts") |
| 45 | + if not isinstance(value, dict): |
| 46 | + if not isinstance(value, list) or not all( |
| 47 | + isinstance(item, dict) for item in value |
| 48 | + ): |
| 49 | + raise ValidationError("Field should be dict or list of dicts") |
| 50 | + return super()._deserialize(value, attr, data, **kwargs) |
64 | 51 |
|
65 | 52 |
|
66 | 53 | class UriOrDictField(StrOrDictField): |
67 | 54 | """URI or Dict field for Marshmallow.""" |
68 | 55 |
|
69 | | - def __init__(self, *args, **kwargs): |
70 | | - """Initialize new UriOrDictField instance.""" |
71 | | - super().__init__(*args, **kwargs) |
72 | | - |
73 | | - # Insert validation into self.validators so that multiple errors can be stored. |
74 | | - self.validators.insert(0, self._uri_validator) |
75 | | - |
76 | | - def _uri_validator(self, value): |
77 | | - # Check if URI when |
| 56 | + def _deserialize(self, value, attr, data, **kwargs): |
78 | 57 | if isinstance(value, str): |
79 | | - return Uri()(value) |
| 58 | + # Check regex |
| 59 | + Uri()(value) |
| 60 | + return super()._deserialize(value, attr, data, **kwargs) |
80 | 61 |
|
81 | 62 |
|
82 | 63 | class IntEpoch(Range): |
@@ -775,7 +756,7 @@ def __call__(self, value): |
775 | 756 | except ValidationError: |
776 | 757 | raise ValidationError( |
777 | 758 | f"credential subject id {value[0]} must be URI" |
778 | | - ) |
| 759 | + ) from None |
779 | 760 |
|
780 | 761 | return value |
781 | 762 |
|
|
0 commit comments