Skip to main content

Latest Version Supported Python versions License

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

Metadata

Release files for flask-ratify 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-ratify 0.1.3
File Size Uploaded
flask-ratify-0.1.3.tar.gz 9.3 kB Details

Release files / flask-ratify-0.1.3.tar.gz

Download URL flask-ratify-0.1.3.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1d837839fc97f618487991d171da2041a85672dca458843972a255d784c95fb6
BLAKE2b-256 checksum
How to use checksums
b89de2eb783566ff3a2c4b14ba9447bf77b0ba31d7fd1e414d230f14eccb5d95
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.1.3 This release

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page