A Flask extension adding a decorator for request validation based on jsonschema
Project description
A Flask extension for ratifying (validating) requests
This package uses the jsonschema for validating the requests. A package specific schema has been implemented on top of jsonschema to validate headers, args, and json_body.
Installation
Install the extension with using pip, or easy_install.
$ pip install -U flask-ratify
Usage
This package exposes a Flask extension which can enforce validations on selected routes. Routes can be specified as regexes as well. The package also contains a decorator, for those who prefer this approach.
flask-ratify returns status 400
Simple Usage
from flask import Flask
from flask_ratify import FlaskRatify
users_schema = {
"POST": {
"headers": {
"type": "object",
"properties": {
"authorization": {"type": "string"}
},
"required": ["authorization"]
},
"json_body": {
"type": "object",
"properties": {
"user_id": {"type": "string"},
"email": {"type": "string", "format": "email"}
},
"required": ["user_id", "email"],
"additionalProperties": False,
},
"args": {
"type": "object"
# add further validations if needed
}
},
"GET": {
# Add validations for GET
}
}
schema = {
"/users": users_schema
}
app = Flask(__name__)
FlaskRatify(app, schema)
@app.route("/users", methods=["POST", "GET"])
def create_user():
= request.args.get("
Schema
flask-ratify schema follows a simple model of it’s own on top of json-schema. For better understanding of json-schema, read
{
"endpoint": {
"http method": {
"headers": {
"type": "object" # For headers this is always object
"properties": {
"header_name": {"type": "string"} # header names should be lower cased,
# rest any jsonschema element can be used
# ...
},
"required": ["header1", "header2"], # Optional
"additionalProperties": False, # Boolean, Optional
},
"args": {
"type": "object" # For args this is always object
"properties": {
"arg_name": {"type": "string"} # any jsonschema element can be used
# ...
},
"required": ["arg1", "arg2"], # Optional
"additionalProperties": False, # Boolean, Optional
},
"json_body": {
"type": "object|array|..." # json_body can follow any type as per jsonschema
"properties": {
"field_name": {"type": "string"} # any jsonschema element can be used
# ...
},
"required": ["field1", "field2"], # Optional
"additionalProperties": False, # Boolean, Optional
}
}
}
}
Troubleshooting
If things aren’t working as you expect, enable logging to help understand what is going on under the hood, and why.
logging.getLogger('flask_').level = logging.DEBUG
TODO
Test cases
Schema validation for debugging
Automatic API documentation generation from schema
Contributing
Questions, comments or improvements? Please create an issue on Github
For code contributions, please create an issue and raise a pull request.
Credits
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file flask-ratify-0.1.3.tar.gz
.
File metadata
- Download URL: flask-ratify-0.1.3.tar.gz
- Upload date:
- Size: 9.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d837839fc97f618487991d171da2041a85672dca458843972a255d784c95fb6 |
|
MD5 | 5719f849236bd87551af5cfcce71b1e5 |
|
BLAKE2b-256 | b89de2eb783566ff3a2c4b14ba9447bf77b0ba31d7fd1e414d230f14eccb5d95 |