nomeyer
2/9/2016 - 8:03 PM

Decorator to validate post requests in Flask

Decorator to validate post requests in Flask

from functools import wraps
from flask import request, abort

def validate_post(validator, status_code):
    """Validate the POST request with the given validator function.
    Abort with the given status_code upon validation failure.
    """
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            if not validator(request):
                abort(status_code)
            return func(*args, **kwargs)
        return wrapper
    return decorator