Skip to main content

Flask-Reqcheck

Flask-Reqcheck lets you validate requests in your Flask applications. With a simple decorator and some Pydantic models, you can quickly validate incoming request bodies, query parameters, and url path parameters, reducing boilerplate code and minimising errors.

Installation

Run the following (preferably inside a virtual environment):

pip install flask-reqcheck

Usage

Flask-Reqcheck is very straightforward to use. The main objects of interest are the ReqCheck extension class, the @validate decorator, and the get_valid_request function.

The ReqCheck extension is initialised like all other Flask extensions during the application setup (see example below). However it is only used for convenience to register a default validation error handler - it is not currently an essential step and can be skipped entirely in favour of your own error handler.

Initialise the extension:

from flask import Flask
from flask_reqcheck import ReqCheck

app = Flask(__name__)
reqcheck = ReqCheck(app)

Or do that with the factory approach and lazy initialisation:

from flask import Flask
from flask_reqcheck import ReqCheck

reqcheck = ReqCheck()

def init_app():
    app = Flask(__name__)
    reqcheck.init_app(app)
    ...
    return app()

The validate decorator is for annotating flask route functions. When you do this, you provide a Pydantic model for the components of the HTTP request that you would like to validate, such as body, query, path, etc. If the request inputs fail to match the corresponding model then a HTTP error is raised.

Aside from @validate, you can use the more specific decorators - @validate_body, @validate_form, @validate_path, @validate_query, etc (see the API reference).

The get_valid_request is a helper function for use within the Flask route function. When using @validate, a new instance of the ValidRequest class will be created and stored for the current request context. We can use get_valid_request to retrieve that object and access its attributes, which correspond to the different HTTP components that were validated.

For example:

from flask_reqcheck import validate, get_valid_request
from pydantic import BaseModel

# Write a class (with Pydantic) to represent the expected data
class BodyModel(BaseModel):
    a: str
    b: int
    c: float
    d: uuid.UUID
    arr: list[int]

@app.post("/body")
@validate(body=BodyModel)  # Decorate the view function
def request_with_body():
    vreq = get_valid_request()  # Access the validated data
    return vreq.to_dict()

First, we import validate and get_valid_request from Flask-Reqcheck. Then we create a custom model using Pydantic’s BaseClass - in this example, it is a simple model for the expected request body. Then you annotate the Flask route function with @validate, providing our model of the request body. Finally, within our route function’s logic, we access the instance of the ValidRequest class and assign it to a variable using vreq = get_valid_request(). We could then call print(vreq.body) to obtain the instance of our request body model.

More specific decorators can also be used:

  • @validate_body
  • @validate_form
  • @validate_path
  • @validate_query

For a full example, see the examples directory in the Flask-Reqcheck repository.

Contributing

Clone the repo, pip install locally, then make the changes. Please do the following:

  • Branch off of the develop branch to work on changes
  • Use the /feature/{feature name} or /bugfix/{bugfix name} format for branch names
  • PR from your branch into develop
  • Use the Black formatter along with isort to keep the codebase clean. Before making a PR:
    • python -m black .
    • python -m isort .
  • Update the docs where necessary - the make html command from the /docs directory might be enough for minor changes. Otherwise, see how they are structured and make changes accordingly. Given that the focus is on just a few functions, the rest of the API is only documented in the code itself so it might not be necessary to include that in the main docs html.
  • Use numpy style docstrings
  • Write tests - PRs will fail if the tests fail

License

MIT

Metadata

Release files for flask-reqcheck 0.0.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-reqcheck 0.0.11
File Size Uploaded Provenance Actions
flask_reqcheck-0.0.11.tar.gz 11.9 kB Without provenance Details for flask_reqcheck-0.0.11.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flask-reqcheck 0.0.11
File Interpreter ABI Platform Actions
flask_reqcheck-0.0.11-py3-none-any.whl Python 3 none any Details for flask_reqcheck-0.0.11-py3-none-any.whl

Total release size: 22.4 kB

Release files / flask_reqcheck-0.0.11.tar.gz

Download URL flask_reqcheck-0.0.11.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
73702edc4a298593af04d299eb973a3ff04665e19297d21713fac2d591d750c3
BLAKE2b-256 checksum
How to use checksums
12291276a6a7062f2a9859d6c0447b2c4406e2fe5af1ff211e3472dcdfedb4ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.6

Release files / flask_reqcheck-0.0.11-py3-none-any.whl

Download URL flask_reqcheck-0.0.11-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8f21f88e0ba156e33be487d69613f1c2d990677313bfb9a6a8cb35009996982e
BLAKE2b-256 checksum
How to use checksums
df940a56181645613a2a04ef285ce4c04645db702294eb977055c03785109c01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.6

Security for flask-reqcheck 0.0.11

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page