使用Webargs Flaskparser验证URL路径中的变量参数和查询参数

时间:2018-08-05 17:38:19

标签: python python-3.x flask webargs

我正在尝试验证所有发送到我的api的数据。我的url结构中包含一个变量/api/v2/users/<string:username>/collections/,以及实际的查询字符串参数,所有这些参数都需要通过验证。

完整网址如下: https://127.0.0.1:5000/api/v2/users/<string:username>/collections/?page=5&per_page=10

需要验证的两个变量是:username pageper_page。简单的解决方案是更改url结构,但是我想知道是否有可能实现我所需要的并保持当前的简单性,而无需在资源类中添加其他验证。如果有可能,您该怎么办?

class UserCollections(Resource):
    @use_args({
        'username': fields.Str(
            required=True,
            validate=username_length,
            error_messages=dict(
                required='Username is required.',
                validator_failed='Username can be between 3 and 25 characters.',
            )
        ),
        'page': fields.Int(
            #required=True,
            missing=1,
            validate=feed_minmax_pages,
            error_messages=dict(
                validator_failed='Maximum number of pages reached.',
            )
        ),
        'per_page': fields.Int(
            #required=True,
            missing=5,
            validate=validate.Range(min=5,max=25),
            error_messages=dict(
                validator_failed='Test number of pages reached.',
            )
        ),
    }, locations=('query',))

    def get(self, args, username):
        print(args)
        print(username)

        return default_schema(
            data={},
            http_status=200
        )

运行代码时,我得到username的验证错误,因为它在args中不存在。

1 个答案:

答案 0 :(得分:2)

闲逛了一会儿,我遇到了解决方案,现在在webargs flaskparser documentation中看到了

除了参数中的其他use_args location参数之外,还可以轻松使用此参数。看来请求方法函数getpost等仍然需要您传入该url变量。在我的情况下是<username>

class UserCollections(Resource):
    @use_args({
        'username': fields.Str(
            location='view_args',
            required=True,
            validate=username_length,
            error_messages=dict(
                required='Username is required.',
                validator_failed='Username can be between 3 and 25 characters.',
            )
        ),
        'page': fields.Int(
            location='query',
            missing=1,
            validate=feed_minmax_pages,
            error_messages=dict(
                validator_failed='Maximum number of pages reached.',
            )
        ),
        'per_page': fields.Int(
            location='query',
            missing=5,
            validate=validate.Range(min=5,max=25),
            error_messages=dict(
                validator_failed='Test number of pages reached.',
            )
        ),
    })

    def get(self, args, username):
        print(args) # access with args['username']
        print(username) # would be nice to not have a second of the same var

        return default_schema(
            data={},
            http_status=200
        )