validation.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:flusk 作者: dimmg 项目源码 文件源码
def schema(path=None):
    """Validate the request payload with a JSONSchema.

    Decorator func that will be used to specify
    the path to the schema that the route/endpoint
    will be validated against.

    :param path: path to the schema file
    :type path: string
    :returns: list of errors if there are any
    :raises: InvalidAPIRequest if there are any errors

    ex:

    @schema('/path/to/schema.json')
    @app.route('/app-route')
    def app_route():
        ...
    """
    def decorator(func):
        @wraps(func)
        def wrapped(*args, **kwargs):
            _path = path.lstrip('/')
            schema_path = os.path.join(SCHEMAS_PATH, request.blueprint, _path)
            payload = get_request_payload(request.method)(request)

            errors = validate_schema(payload, get_schema(schema_path))
            if errors:
                raise InvalidAPIRequest(message=errors)

            return func(*args, **kwargs)
        return wrapped
    return decorator
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号