Basic JSON Schema Validator for the Flask web framework.
Project description
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"
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for Flask-JsonSchemaValidator-0.3.0.linux-x86_64.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d9f619ffeb8bb68f4787e93e713afb7c4681c2dae37a81db744e1adaaad3250b |
|
MD5 | eea98c5300e4ea3b140e07e4928c7d57 |
|
BLAKE2b-256 | 0b925100a0bd466cf3ed1401788a5fc6fd843ea6cd6d1860ed1d9fe327b044cb |
Close
Hashes for Flask_JsonSchemaValidator-0.3.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07f44cfcd16cd93a69ed3485b364c4a66e4df82871f9d6fff134f7497a7d6250 |
|
MD5 | 9dae6d32c806027df863fd3fa19cc01d |
|
BLAKE2b-256 | 217f3333d3d440ef2033fd105f1df976e98481a1808db547cfecdd616e14081e |