Provides a wrapper which provides valid json to Resource methods.
Switch branches/tags
Nothing to show
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
flask_restful_jsonschema readme (more info), pin to 0.1.1 Sep 29, 2016
.gitignore Initial commit Sep 29, 2016
LICENSE Initial commit Sep 29, 2016
README.md Update README.md Sep 29, 2016
requirements.txt first real commit Sep 29, 2016
setup.py pypi Sep 29, 2016

README.md

flask_restful_jsonschema

PyPI Version

Add the @validate_json decorator and schema class constant to flask_restful.Resource methods (post, get, etc.) in order to validate requests meet the jsonschema.

Ensure JSON request matches schema specified in the class the wrapped method belongs to, provide that valid JSON to the method, or abort 400 with the validation error message.

from flask_restful_jsonschema import validate_json


class Users(flask_restful.Resource):
    SCHEMA_POST = {
        "type": "object",
        "properties": {
            "email": {"type": "string"},
            "password": {"type": "string"},
        },
        "required": ["email", "password"],
    }
    SCHEMA_PUT = {
        "type": "object",
        "properties": {
            "email": {"type": "string"},
            "password": {"type": "string"},
        },
    }
    SCHEMA_GET = {
        "type": "object",
        "properties": {
            "email": {"type": "string"},
        },
        "required": ["email"],
    }

    @validate_json
    def post(self, json_request):
        json_request["email"]
        json_request["password"]

    @validate_json
    def put(self, json_request):
        json_request.get("email")
        json_request.get("password")

    @validate_json
    def get(self, json_request):
        json_request["email"]