Validate Flask request parameters effortlessly with Pydantic
Project description
About
flask_typed_routes is a Python library designed to validate Flask requests effortlessly with Pydantic.
Features
- Type Safety: Automatically validates request parameters based on type annotations.
- Easy Integration: Simple decorator syntax for applying validation to Flask routes.
- Error Handling: Automatically returns meaningful error responses for validation failures.
Requirements
- Python 3.10+
- Pydantic 2.0+
- Flask
Installation
To install flask_typed_routes using pip, run the following command:
pip install flask_typed_routes
Getting Started
Example of a simple Flask application using flask_typed_routes:
Create a file posts.py with:
import flask
import flask_typed_routes as flask_tpr
app = flask.Flask(__name__)
flask_tpr.FlaskTypeRoutes(app)
@app.route('/posts/<user>/')
@flask_tpr.typed_route
def read_user_posts(user: str, skip: int = 0, limit: int = 10):
# Parameters not included in the "path" are automatically treated as "query" parameters.
data = {
'user': user,
'skip': skip,
'limit': limit,
}
return flask.jsonify(data)
Run the server with:
flask --app posts run
Data conversion:
Open your browser and go to 127.0.0.1:5000/posts/myuser/?skip=20 You will see the JSON response as:
{
"limit": 10,
"skip": 20,
"user": "myuser"
}
Data validation:
Open your browser and go to 127.0.0.1:5000/posts/myuser/?skip=abc
You will see the JSON response with the error details because the skip parameter is not an integer:
{
"errors": [
{
"input": "abc",
"loc": [
"query",
"skip"
],
"msg": "Input should be a valid integer, unable to parse string as an integer",
"type": "int_parsing",
"url": "https://errors.pydantic.dev/2.9/v/int_parsing"
}
]
}
Documentation
For more detailed information and usage examples, refer to the project documentation
Development
To contribute to the project, you can run the following commands for testing and documentation:
Running Unit Tests
Install the development dependencies and run the tests:
(env)$ pip install -r requirements-dev.txt # Skip if already installed
(env)$ python -m pytest tests/
(env)$ python -m pytest --cov # Run tests with coverage
Building the Documentation
To build the documentation locally, use the following commands:
(env)$ pip install -r requirements-doc.txt # Skip if already installed
(env)$ mkdocs serve # Start live-reloading docs server
(env)$ mkdocs build # Build the documentation site
License
This project is licensed under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flask_typed_routes-0.0.1b3.tar.gz.
File metadata
- Download URL: flask_typed_routes-0.0.1b3.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2344cb9af4de2d73355a77bbd0bfc42fa78319fc2d51a6e00f5311e594204a48
|
|
| MD5 |
5780600a19c18d1892be718922881d08
|
|
| BLAKE2b-256 |
b9183e6e5e91282b27e06e7c6e98ff26d07ffcd3377d35eb17438845d9ced8cb
|
File details
Details for the file flask_typed_routes-0.0.1b3-py3-none-any.whl.
File metadata
- Download URL: flask_typed_routes-0.0.1b3-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a0e5a9e789b86f72e2b938977293dfe05bebdf6fdb3e30d583a95ec6cb89fcc
|
|
| MD5 |
4b2077639d0b2ad0bdf65822aae08ba9
|
|
| BLAKE2b-256 |
978c6efad28b0c3d9401fd0778f9c18b8061fdf2950999a35a8b0dc049635dca
|