Giter Site home page Giter Site logo

flask-jsonschemavalidator's Introduction

Flask-JsonSchemaValidator

Basic JSON Schema Validator for the Flask web framework.

Usage

Basic usage is to apply the @validate decorator to a route. If request validates the route will be called as normal. If the request doesn't validate an error message will be genrated and a 400 BAD REQUEST will be returned with the error message in the body.

validate(schema, force=False, json_cache=True)

schema: jsonschema to validate against

force: try to validate request if Content-Type is not applciation/json

json_cache: cache json with flask.request.get_json

Example

from flask_jsonschema import validate

@app.route("/", methods=['POST'])
@validate({
	        'type': 'object',
	        'properties': {
	            'foo': {'type': 'string'},
	            'bar': {'type': 'number'},
	        },
	        'required': ['bar', 'foo'],
	    })
def index_post():
    return flask.jsonify(
            time=time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
            )

Valid Request

Request:

POST / HTTP/1.1
Content-Type: application/json
Host: localhost:5000
Content-Length: 26

{"foo":"String","bar":123}

Response:

HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 35
Server: Werkzeug/0.10.4 Python/3.4.3
Date: Thu, 27 Aug 2015 04:05:24 GMT

{
  "time": "2015-08-27 04:05:24"
}

Invalid Request

Request:

POST / HTTP/1.1
Content-Type: application/json
Host: localhost:5000
Content-Length: 37

{"foo":"String","bar":"Not a number"}

Response:

HTTP/1.0 400 BAD REQUEST
Content-Type: application/json
Content-Length: 145
Server: Werkzeug/0.10.4 Python/3.4.3
Date: Thu, 27 Aug 2015 03:53:39 GMT

{
  "error_message": "'Not a number' is not of type 'number'",
  "error_path": "(root).bar",
  "status": 400,
  "status_message": "Bad Request"
}

flask-jsonschemavalidator's People

Contributors

wesclemens avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

python3pkg

flask-jsonschemavalidator's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.