Last active
February 10, 2016 20:51
-
-
Save nomeyer/d66283097b473c770d75 to your computer and use it in GitHub Desktop.
Decorator to validate post requests in Flask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment