-
-
Notifications
You must be signed in to change notification settings - Fork 629
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a way to trim whitespace characters #1391
Comments
You could use a custom field. from marshmallow import fields, Schema
class TrimmedString(fields.String):
def _deserialize(self, value, *args, **kwargs):
if hasattr(value, 'strip'):
value = value.strip()
return super()._deserialize(value, *args, **kwargs)
class ArtistSchema(Schema):
name = TrimmedString(required=True)
print(ArtistSchema().load({"name": " David "}))
# {'name': 'David'} You could also use field composition if you want to trim other field types. from marshmallow import fields, Schema
class Trim(fields.Field):
def __init__(self, inner, *args, **kwargs):
self.inner = inner
super().__init__(*args, **kwargs)
def _bind_to_schema(self, field_name, parent):
super()._bind_to_schema(field_name, parent)
self.inner._bind_to_schema(field_name, parent)
def _deserialize(self, value, *args, **kwargs):
if hasattr(value, 'strip'):
value = value.strip()
return self.inner._deserialize(value, *args, **kwargs)
def _serialize(self, *args, **kwargs):
return self.inner._serialize(*args, **kwargs)
class ArtistSchema(Schema):
name = Trim(fields.String(), required=True)
email = Trim(fields.Email())
print(ArtistSchema().load({"name": " David ", "email": " [email protected] "}))
# {'name': 'David', 'email': '[email protected]'} That said, I'm going to keep this issue open because this may be a common enough use case to justify adding to marshmallow core. Feedback welcome. |
@sloria thanks for your quick reply, the example you gave above can solve my problem, it's great. |
Copying #1397 (comment) here for discussion:
|
|
I don't think this is a convincing reason, because for number type, whether or not we provide the strip_whitespace parameter doesn't make any difference to the result.
|
Sure, Python's |
Bool types do have this problem. |
I'm not sure. Perhaps we could have a |
Decorator version: def trim_before(wrapped):
def wrapper(self, value, *args, **kwargs):
if hasattr(value, 'strip'):
value = value.strip()
return wrapped(self, value, *args, **kwargs)
return wrapper
from marshmallow import fields
fields.String._deserialize = trim_before(fields.String._deserialize) |
Thanks for the example. Is there any reason to add the condition |
Haven't you had any definition about the implementation of this validation in the lib yet? |
Wouldn't it make sense to provide a generic |
one issue with composable fields like perhaps a better way to solve this is to allow validators to return changed values, similar to pydantic.
that way you could easily apply a pipeline of transformations to any field. from marshmallow import Schema, fields
def strip_whitespace(value: str) -> str:
return value.strip()
def uppercase(value: str) -> str:
return value.upper()
class ArtistSchema(Schema):
name = fields.Str(validate=(strip_whitespace, uppercase))
print(ArtistSchema().load({"name": " foo "}))
# {"name": "FOO"} this would be a breaking change so would need to go in 4.0, but i think it's a decent way to solve this use case update: #2786 implements this |
Like the trim_whitespace parameter in the django rest framework.
The text was updated successfully, but these errors were encountered: