You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like the ability to define a marshmallow.fields.String that supports case transformation during (de)serialization.
Sometimes I've found it necessary to convert input/output to uppercase and lowercase. Most commonly, I convert all hash digests to lowercase regardless of the case I get from a user.
I generally solve this with something simple:
classStringLower(fields.String):
""" A ``marshmallow.field.String`` that is converted to lowercase """case='lower'def_deserialize(self, value, attr, data, **kwargs) ->AnyStr:
try:
returngetattr(value, self.case)()
exceptAttributeErrorase:
raisefields.ValidationError("Input must be a string") fromedef_serialize(self, value, attr, obj, **kwargs) ->AnyStr:
try:
returngetattr(value, self.case)()
exceptAttributeErrorase:
raisefields.ValidationError("Input must be a string") fromeclassStringUpper(StringLower):
""" A ``marshmallow.field.String`` that is converted to uppercase """case='upper'
It would be nice to be able to define a String field that did this automatically:
I would like the ability to define a
marshmallow.fields.String
that supports case transformation during (de)serialization.Sometimes I've found it necessary to convert input/output to uppercase and lowercase. Most commonly, I convert all hash digests to lowercase regardless of the case I get from a user.
I generally solve this with something simple:
It would be nice to be able to define a
String
field that did this automatically:The text was updated successfully, but these errors were encountered: