Search code examples
pythonflaskflask-restfulmarshmallowwebargs

Can webargs / Marshmallow in Python modify a field, not just validate it?


I'm using Flask with flask-restful and webargs (which uses Marshmallow as its backend). Currently I'm able to pull in fields I want with this:

class AddGroup(Resource):
    args = {
        'name': fields.Str(missing=None),
        'phone': fields.Str(missing=None),
    }

    @use_args(args)
    def get(self, args):
        name = args['name'].strip()
        # ... some GET-related code ...

    @use_args(args)
    def post(self, args):
        name = args['name'].strip()
        # ... some POST-related code ...

So far so good. But what I'd really like to do is make sure that args['name'] comes into the various methods ("post", "get", etc.) with whitespace already stripped, so I don't have to process each variable manually each time. (stripping whitespace is just an example - it may be some other simple or complex transformation)

Is there a way (either by overriding the String field, defining my own field, or whatever) that will allow the args to be pre-processed before they arrive in the class methods?


Solution

  • Since webargs are using marshmallow to make schema, you can use pre_load or post_load. There is example with "slugify" a string in the docs of marshmallow:

    from marshmallow import Schema, fields, pre_load
    
    class UserSchema(Schema):
        name = fields.Str()
        slug = fields.Str()
    
        @pre_load
        def slugify_name(self, in_data, **kwargs):
            in_data['slug'] = in_data['slug'].lower().strip().replace(' ', '-')
            return in_data
    
    schema = UserSchema()
    result = schema.load({'name': 'Steve', 'slug': 'Steve Loria '})
    result['slug']  # => 'steve-loria'
    

    You can see detailed docs here: https://marshmallow.readthedocs.io/en/latest/extending.html#pre-processing-and-post-processing-methods