Falcon middleware to serialize/deserialize JSON with built-in input validation
Project description
Falcon middleware to serialize/deserialize JSON with built-in input validation.
pip install falcon-jsonify
Add the middleware into your project:
import falcon_jsonify
falcon.API(middleware=[falcon_jsonify.Middleware(help_messages=True)])
Get Started
Responses
resp.json = {"my_field": "Hello World"}
Requests
value = req.get_json('my_field') # required field
Response 400 Bad Request is returned if the field does not exist in the request body.
Full deserialized dict can be accesed at req.json (without validations), e.g. req.json['my_field'].
Built-in validations
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)
Response 400 Bad Request is returned if any validation fail.
Additional parameters
default, match
# 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="[^@]+@[^@]+\.[^@]+")
Error responses
Example:
400 Bad Request
{
"title": "Validation error",
"description": "Minimum value for 'age' is '18'"
}
For proprietary APIs on production environment set Middleware(help_messages=False) to hide such error messages (missing fields, validation checks, malformed JSON). For public APIs it’s nice to keep them enabled.
Project details
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
Hashes for falcon_jsonify-0.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | def0384f43d359bacd70e88c6784c7f830d2266335f0305d1f497d30c8f67d1a |
|
MD5 | 93747d45098d3188ce444e43dff21de2 |
|
BLAKE2b-256 | d61985d1efdb847c73ba88e3b509aa1dd0bc5186120dd01d0df1c6aea6944a17 |