Skip to content

Instantly share code, notes, and snippets.

@nomeyer
Last active February 10, 2016 20:51
Show Gist options
  • Save nomeyer/d66283097b473c770d75 to your computer and use it in GitHub Desktop.
Save nomeyer/d66283097b473c770d75 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment