flask-ratify
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.