Skip to main content

Validate Flask JSON request data with schema files and route decorators

Project description

Flask JSON Schema Validator

Validate Flask JSON request data with schema files and route decorators.


Daniel 'Vector' Kerr (


Refer to LICENSE.txt.


pip install flask-jsonschema-validator

Sample Usage

Python Code

from flask import Flask, request, jsonify
from flask_jsonschema_validator import JSONSchemaValidator

app = Flask()
JSONSchemaValidator( app = app, root = "schemas" )

# Define a normal flask route, and then apply the `validate` decorator.
# Look for the `users.json` file, and use the `register` key as the schema source.
@app.route( '/register', methods = [ 'POST' ] )
@app.validate( 'users', 'register' )
def routeRegister():
  user = request.json
  return jsonify( user )

if __name__ == '__main__': port = 8080 )

Schema File


  "register": {
    "type": "object",
    "properties": {
      "name": { "type": "string", "minLength": 2, "maxLength": 100 },
      "email": { "type": "string", "format": "email" },
      "password": { "type": "string", "minLength": 8, "maxLength": 32 }
    "required": [ "name", "email", "password" ]

Run Server


POST Data to Server

POST /register HTTP/1.0
Content-Type: application/json
Content-Length: 76

	"name": "fred",
	"email": "",
	"password": "frediscool"

Handling Validation Errors

If the data validates correctly then the server will respond with the POST data as a JSON object.

If the data fails to validate, a jsonschema.ValidationError exception will be raised.

To handle the exception, you could register a Flask errorhandler. For example:

import jsonschema
from flask import Response

@app.errorhandler( jsonschema.ValidationError )
def onValidationError( e ):
  return Response( "There was a validation error: " + str( e ), 400 )

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for flask-jsonschema-validator, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size flask_jsonschema_validator-0.0.3-py2-none-any.whl (3.4 kB) File type Wheel Python version py2 Upload date Hashes View
Filename, size flask-jsonschema-validator-0.0.3.tar.gz (2.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page