flask-reqcheck


Nameflask-reqcheck JSON
Version 0.0.10 PyPI version JSON
download
home_pageNone
SummaryRequest validation for Flask applications using Pydantic.
upload_time2024-09-09 19:03:52
maintainerNone
docs_urlNone
authorLewis Dunne
requires_python>=3.11
licenseNone
keywords python flask validation
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Flask-Reqcheck

**Flask-Reqcheck** lets you validate requests in your Flask applications. With a simple 
decorator and some [Pydantic](https://docs.pydantic.dev/latest/) models, you can quickly 
validate incoming request bodies, query parameters, and url path parameters, reducing 
boilerplate code and minimizing errors.

## Installation

Run the following (preferably inside a virtual environment):

```sh
pip install flask-reqcheck
```

## Usage

Flask-Reqcheck is very straightforward to use. The main two objects of interest are the `@validate` decorator and the `get_valid_request` function.

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:

```python
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`

More to come.

For a full example, see the [examples directory in the Flask-Reqcheck repository](/example/).

## 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](https://black.readthedocs.io/en/stable/) formatter along with [isort](https://pycqa.github.io/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

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "flask-reqcheck",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "Python, Flask, validation",
    "author": "Lewis Dunne",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/b5/60/3904746988e2dd2453e6dad922311847279c2855807a5f8810c1ccbc0b02/flask_reqcheck-0.0.10.tar.gz",
    "platform": null,
    "description": "# Flask-Reqcheck\n\n**Flask-Reqcheck** lets you validate requests in your Flask applications. With a simple \ndecorator and some [Pydantic](https://docs.pydantic.dev/latest/) models, you can quickly \nvalidate incoming request bodies, query parameters, and url path parameters, reducing \nboilerplate code and minimizing errors.\n\n## Installation\n\nRun the following (preferably inside a virtual environment):\n\n```sh\npip install flask-reqcheck\n```\n\n## Usage\n\nFlask-Reqcheck is very straightforward to use. The main two objects of interest are the `@validate` decorator and the `get_valid_request` function.\n\nThe `validate` decorator is for annotating flask route functions. When you do this, you provide a Pydantic model for the components of the HTTP \nrequest that you would like to validate, such as `body`, `query`, `path`, etc. If the request inputs fail to match the corresponding model then \na HTTP error is raised. \n\nAside from `@validate`, you can use the more specific decorators - `@validate_body`, `@validate_form`, `@validate_path`, \n`@validate_query`, etc (see the API reference).\n\nThe `get_valid_request` is a helper function for use *within* the Flask route function. When using `@validate`, a new instance of the `ValidRequest` class \nwill 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 \nto the different HTTP components that were validated.\n\nFor example:\n\n```python\nfrom flask_reqcheck import validate, get_valid_request\nfrom pydantic import BaseModel\n\n# Write a class (with Pydantic) to represent the expected data\nclass BodyModel(BaseModel):\n    a: str\n    b: int\n    c: float\n    d: uuid.UUID\n    arr: list[int]\n\n@app.post(\"/body\")\n@validate(body=BodyModel)  # Decorate the view function\ndef request_with_body():\n    vreq = get_valid_request()  # Access the validated data\n    return vreq.to_dict()\n```\n\nFirst, we import `validate` and `get_valid_request` from Flask-Reqcheck. Then we create a custom model using Pydantic\u2019s `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\u2019s 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.\n\nMore specific decorators can also be used:\n- `@validate_body`\n- `@validate_form`\n- `@validate_path`\n- `@validate_query`\n\nMore to come.\n\nFor a full example, see the [examples directory in the Flask-Reqcheck repository](/example/).\n\n## Contributing\n\nClone the repo, pip install locally, then make the changes. Please do the following:\n\n- Branch off of the `develop` branch to work on changes\n- Use the `/feature/{feature name}` or `/bugfix/{bugfix name}` format for branch names\n- PR from your branch into `develop`\n- Use the [Black](https://black.readthedocs.io/en/stable/) formatter along with [isort](https://pycqa.github.io/isort/) to keep the codebase clean. Before making a PR:\n    - `python -m black .`\n    - `python -m isort .`\n- 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.\n- Use `numpy` style docstrings\n- Write tests - PRs will fail if the tests fail\n\n\n## License\n\nMIT\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Request validation for Flask applications using Pydantic.",
    "version": "0.0.10",
    "project_urls": null,
    "split_keywords": [
        "python",
        " flask",
        " validation"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3599bc2e44d98040a184252282454df24c00fff12ca6349e5537ff97231acaa7",
                "md5": "b234fa60eb01afeb5e1ce89158c07a70",
                "sha256": "82763c25e4fe8f9a9df0bc6524838d71f2e3bcf90a668330160a3702777feee1"
            },
            "downloads": -1,
            "filename": "flask_reqcheck-0.0.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b234fa60eb01afeb5e1ce89158c07a70",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 9446,
            "upload_time": "2024-09-09T19:03:51",
            "upload_time_iso_8601": "2024-09-09T19:03:51.391383Z",
            "url": "https://files.pythonhosted.org/packages/35/99/bc2e44d98040a184252282454df24c00fff12ca6349e5537ff97231acaa7/flask_reqcheck-0.0.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b5603904746988e2dd2453e6dad922311847279c2855807a5f8810c1ccbc0b02",
                "md5": "e6cac3bdd1ed33727db635f9b28ae2ad",
                "sha256": "4da2677e7ea280f3af1f0fefe1fa71313dc87ebc5aef6c6baf85950dba8126e6"
            },
            "downloads": -1,
            "filename": "flask_reqcheck-0.0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "e6cac3bdd1ed33727db635f9b28ae2ad",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 10880,
            "upload_time": "2024-09-09T19:03:52",
            "upload_time_iso_8601": "2024-09-09T19:03:52.346957Z",
            "url": "https://files.pythonhosted.org/packages/b5/60/3904746988e2dd2453e6dad922311847279c2855807a5f8810c1ccbc0b02/flask_reqcheck-0.0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-09 19:03:52",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "flask-reqcheck"
}
        
Elapsed time: 0.35420s