I think better use json schema instead:
from jsonschema import validate, ValidationError
schema = {
'type': 'object',
'properties': {
'title': {
'type': 'string',
},
'description': {
'type': 'string',
},
'done': {
'type': 'boolean',
},
},
}
try:
validate(request.get_json(), schema)
except ValidationError:
abort(400)
Also request.json
is deprecated if you use flask>=0.10
, and better use request.get_json()
: http://flask.pocoo.org/docs/api/#flask.Request.json.