How the `@validate` decorator works
masterThe @validate decorator integrates Pydantic models with Flask routes to validate incoming request data. It supports three main types of parameters:
- Query Parameters: Accessed via
request.query_paramsor as a function argument. - Body Parameters (JSON): Accessed via
request.body_paramsor as a function argument. - Form Data: Accessed via
request.form_paramsor as a function argument.
Important: The @validate decorator must be placed after the @app.route decorator (closer to the function definition).
If validation fails, the extension returns a 400 response (by default) containing a JSON object describing the validation errors.
@app.route("/path", methods=["POST"])
@validate()
def my_route(body: MyModel):
# body is already validated and parsed
return {"status": "ok"}