Ge0rg3 / flask-parameter-validation

Get and validate all Flask input parameters with ease.
https://pypi.org/project/Flask-Parameter-Validation/
27 stars 12 forks source link

Passing other decorators along with ValidateParameters() #16

Closed vvkvivekl closed 3 weeks ago

vvkvivekl commented 1 year ago

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 commented 1 year ago

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(),
    ):
    # ...
smt5541 commented 2 months ago

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 commented 3 weeks ago

Closing stale question