flask-parameter-validation icon indicating copy to clipboard operation
flask-parameter-validation copied to clipboard

Passing other decorators along with ValidateParameters()

Open vvkvivekl opened this issue 2 years ago • 2 comments

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(),
    ):
    # ...

vvkvivekl avatar Jun 29 '23 16:06 vvkvivekl

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(),
    ):
    # ...

vvkvivekl avatar Jun 29 '23 16:06 vvkvivekl

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"])

smt5541 avatar Jul 01 '24 21:07 smt5541

Closing stale question

smt5541 avatar Aug 13 '24 16:08 smt5541