🅕🅛🅐🅢🅚-🅓🅐🅝🅣🅘🅒
Flask-Dantic is a Python package that would enable users to use Pydantic models for validations and serialization, thus making it easy to link Flask with Pydantic. It can validate the request params, query args and path args.
Also, the package provides a serializer that serializes the database objects using the pydantic models. This comes handy if you are using pydantic models for request and response in Flask.
A single serialize call will take care of validating the returned response as well as serializing it. There are options to include or exclude certain fields or exclude/include fields with null values.
Compatibility
This package is compatible with Python >= 3.6
Installation
Install with pip:
pip install flask-dantic
Examples
Validating body parameters
# Using the Pydantic model for request.
from typing import Optional
from flask import current_app as flask_app, request
from pydantic import BaseModel
from flask_dantic import pydantic_validator
class UserCreateModel(BaseModel):
username: str
age: Optional[int] = None
phone: Optional[str] = None
@flask_app.route("/user/create", methods=["POST"])
@pydantic_validator(body=UserCreateModel) # Pass the model against body kwarg.
def create_user():
"""
Request Json to create user that will be validated against UserModel
{
"username": "Foo",
"age": 42,
"phone": "123-456-7890"
}
"""
user_model = request.body_model
print(user_model.username, user_model.age, user_model.phone)
Change the default validation error status code. Default status code is 422
@flask_app.route("/user/create", methods=["POST"])
# Changing the default validation error status code from default 422 to 400
@pydantic_validator(body=UserCreateModel, validation_error_status_code=400)
def create_user():
"""
Request Json to create user that will be validated against UserModel
{
"username": "Foo",
"age": 42,
"phone": "123-456-7890"
}
"""
user_model = request.body_model
print(user_model.username, user_model.age, user_model.phone)
Validating Query args - request.args
# Using the Pydantic model for request.
from typing import Optional
from flask import current_app as flask_app, request
from pydantic import BaseModel
from flask_dantic import pydantic_validator
# Sample url - https://localhost:5000/user/get?username=Foo&age=42
# Here username and foo are pass are query args
class UserQueryModel(BaseModel):
username: str
age: Optional[int] = None
@flask_app.route("/user/get", methods=["GET"])
@pydantic_validator(query=UserQueryModel) # Pass the model against query kwarg
def get_user():
user_query_model = request.query_model
print(user_query_model.username, user_query_model.age)
Validating URL Path args
# Using the Pydantic model for request.
from flask import current_app as flask_app, request
from pydantic import BaseModel, Field
from flask_dantic import pydantic_validator
# Sample url - https://localhost:5000/user/get/c55926d3-cbd0-4eea-963b-0bcfc5c40d46
# Here the uuid is the dynamic path param.
UUID_REGEX = "[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}"
class UserPathParamModel(BaseModel):
user_id: str = Field(..., regex=UUID_REGEX, description="ID of the user")
@flask_app.route("/user/get/<string:user_id>", methods=["GET"])
@pydantic_validator(path_params=UserPathParamModel) # Pass the model against path_params
def get_user(user_id):
path_param_model = request.path_param_model
print(path_param_model.user_id)
Serialization using Pydantic module and returning the response.
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
db_engine = create_engine(DB_CONNECT_STRING) # DB connection string, ex "sqlite:///my_app.db"
db = Session(db_engine)
from http import HTTPStatus
from typing import Optional
from flask import current_app as flask_app, jsonify
from pydantic import BaseModel
from flask_dantic import serialize, pydantic_validator
class UserResponseModel(BaseModel): # Define the pydantic model for serialization.
username: str
age: Optional[int] = None
phone: Optional[str] = None
@flask_app.route("/user/list", methods=["GET"])
def get_all_users():
users = get_all_users_from_db()
# Pass the db records and pydantic model to serialize method. Set many as True if there are multiple records.
serialized_users = serialize(users, UserResponseModel, many=True) # Serialize call
return jsonify(serialized_users), HTTPStatus.OK
@flask_app.route("/user/get/<string:user_id>", methods=["GET"])
@pydantic_validator(path_params=UserPathParamModel) # Pass the model against path_params
def get_user(user_id):
user = get_single_user_by_id(user_id)
# Pass the db record and pydantic model to serialize method. Many is set to False by default.
user = serialize(user, UserResponseModel) # Serialize call
return jsonify(user), HTTPStatus.OK
Serialization - Dump directly to json. This is useful when you want to return the response as json without flask jsonify.
from flask_dantic import serialize
# Taking the same example from above. Modifying the serialize call.
@flask_app.route("/user/get/<string:user_id>", methods=["GET"])
@pydantic_validator(path_params=UserPathParamModel) # Pass the model against path_params
def get_user(user_id):
user = get_single_user_by_id(user_id)
# Pass the db record and pydantic model to serialize method. Many is set to False by default.
# Serialize call
return serialize(user, UserResponseModel, json_dump=True), HTTPStatus.OK
Tests
Run tests:
pytest
License
Flask-Dantic is released under the MIT License. See the bundled LICENSE file
for details.
Metadata
Release files for flask-dantic 0.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| flask-dantic-0.0.7.tar.gz | 14.3 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| flask_dantic-0.0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.2 kB
Release files / flask-dantic-0.0.7.tar.gz
| Download URL | flask-dantic-0.0.7.tar.gz |
|---|---|
| Size | 14.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7a33654b2d09217fea03cf3959e599ad132a0f9b800c80a888a16f8ce1b3b1ba
|
|
BLAKE2b-256 checksum How to use checksums |
574f4643120ae88bb9b0aa0e226905644a33113b1e1b96f316b376eec9104f54
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.11
|
Release files / flask_dantic-0.0.7-py3-none-any.whl
| Download URL | flask_dantic-0.0.7-py3-none-any.whl |
|---|---|
| Size | 14.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
799c9516bdb20ec22891c7f6fbd94ae715373311b317b8aa1fe57381f930432c
|
|
BLAKE2b-256 checksum How to use checksums |
677b668fd4dbc93f4bebd0fa70d9261e29c950e98e37a92e8b5f2ed3303a6e9e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.11
|
Security for flask-dantic 0.0.7
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.