Skip to main content

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

falcon-jsonify-0.1.1.tar.gz (2.8 kB view hashes)

Uploaded Source

Built Distribution

falcon_jsonify-0.1.1-py2.py3-none-any.whl (5.0 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page