Add DictOrStr field
@@ -125,6 +125,25 @@ class FloatOrStr(fields.Str):
return super(FloatOrStr, self)._deserialize(value=value, attr=attr, data=data)
+class DictOrStr(fields.Str):
+ default_error_messages = {
+ 'invalid': 'Not a valid string or dict.',
+ 'invalid_utf8': 'Not a valid utf-8 string.'
+ }
+
+ def _serialize(self, value, attr, obj):
+ if isinstance(value, Mapping):
+ return value
+
+ return super(DictOrStr, self)._serialize(value=value, attr=attr, obj=obj)
+
+ def _deserialize(self, value, attr, data):
+ if isinstance(value, Mapping):
+ return value
+
+ return super(DictOrStr, self)._deserialize(value=value, attr=attr, data=data)
+
+
class Range(fields.Field):
REQUIRED_KEYS = ['start', 'stop', 'step']
OPTIONAL_KEY = None
