A python package to verify the content of Requests and convert the return values of view functions in Flask applications.
You can install flask_verify
pip install flask_verifyConsider this minimal example, where Message is a simple dataclass.
from flask_verify.verify_json import verify_json_route
from flask import Flask, request
app = Flask(__name__)
@app.route('/example', methods=['POST'])
@verify_json_route(must_contain=('message', 'username'))
def example_route():
message = Message(request.json['username'], request.json['message'])
return message, 200Just by writing a single decorator statement, we have:
- Ensured that the
Requestis of typeapplication/jsonand contains keysmessageandusername, if this is not true, our server will respond with a 400 status code Response explaining the issue to the server, including the name of the first missing key if that exists. - Converted the return type to JSON,
messageobject is an instance of theMessagedataclass, thanks to our decorator, the Response will automatically be converted to a JSON containing the field names and values of the dataclass instance.