flask-parameter-validation
flask-parameter-validation copied to clipboard
Passing other decorators along with ValidateParameters()
I have own custom decorator now sure how to use it along with ValidateParameters() Example code:
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
# ...
return f(current_user, *args, **kwargs)
return decorated
import flask_parameter_validation as fp_validation
# ...
@users_blueprint.route("/user", methods=["POST"])
@token_required
@fp_validation.ValidateParameters()
def update_user(
current_user: AuthUser,
date_of_birth: Optional[str] = fp_validation.Json(),
gender: Optional[str] = fp_validation.Json(),
):
# ...
For now I was able to resolve this issue by passing parameter into kwargs
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
# ...
kwargs["current_user"] = current_user
return f(*args, **kwargs)
return decorated
@users_blueprint.route("/user", methods=["POST"])
@token_required
@fp_validation.ValidateParameters()
def update_user(
current_user: AuthUser = fp_validation.Route(),
date_of_birth: Optional[str] = fp_validation.Json(),
gender: Optional[str] = fp_validation.Json(),
):
# ...
The way that I do this is through the use of the Flask session to store a User's ID, which I ensure is set through a @login_required decorator that redirects the user to authentication if it they aren't authenticated - if they are authenticated, I use the session variable to retrieve the current user with current_user = User.get_by_id(session["user_id"])
Closing stale question