Falcon middleware to serialize/deserialize JSON. (Python 3 and 2)
$ pip install falcon-jsonifyAdd middleware to your app:
import falcon_jsonify
api = falcon.API(middleware=[
falcon_jsonify.Middleware(help_messages=True),
])To disable error messages set help_messages=False.
See example usage.
resp.json = {"my_field": "Hello World"}value = req.get_json('my_field') # required field- Raises response
400 Bad Requestif field does not exist in the request body (JSON). - Full deserialized dict can be accesed at
req.json(without validations), e.g.req.json['my_field'].
dtype,min,max
req.get_json('name', dtype=str, min=1, max=16) # min/max char length
req.get_json('age', dtype=int, min=18, max=99) # min/max numeric value
req.get_json('amount', dtype=float, min=0.0)
req.get_json('approved', dtype=bool)- Raises response
400 Bad Requestwith error message if any validation fails.
default,match(regex)
# make a field optional with default value
req.get_json('country_code', dtype=str, default="USA", max=3, min=3)
# custom validation with Regular Expressions
req.get_json('email', match="[^@]+@[^@]+\.[^@]+")Status code: 400 Bad Request
{
"title": "Validation error",
"description": "Minimum value for 'age' is '18'"
}