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.

Author

Daniel 'Vector' Kerr (vector@vector.id.au)

License

Refer to LICENSE.txt.

Installation

pip install flask-jsonschema-validator

Sample Usage

Python Code

main.py

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__':
  app.run( port = 8080 )

Schema File

schemas/users.json

{
  "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

python main.py

POST Data to Server

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

{
	"name": "fred",
	"email": "fred@foo.com",
	"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.

Source Distribution

flask-jsonschema-validator-0.0.3.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file flask-jsonschema-validator-0.0.3.tar.gz.

File metadata

File hashes

Hashes for flask-jsonschema-validator-0.0.3.tar.gz
Algorithm Hash digest
SHA256 b58ad771f406b672ded6468628243387f4e6fbe824791a55cdd7ed49e548d870
MD5 869ec825faa895a16f23a2386a67a1f8
BLAKE2b-256 e3c2959343c4b4af7fd958f28c1d1d3aba4c9eb830778db8cc2101ea62c4d730

See more details on using hashes here.

File details

Details for the file flask_jsonschema_validator-0.0.3-py2-none-any.whl.

File metadata

File hashes

Hashes for flask_jsonschema_validator-0.0.3-py2-none-any.whl
Algorithm Hash digest
SHA256 65195b1929b02e214f735478bae89551d76bf67b65222ab7c0ad1dab027075bc
MD5 67b7a4421046b6c51e8ef7a153fb3bee
BLAKE2b-256 54277d0dd61b23f246d24c97c0a4fb68fd98c3fcf934f751aa261e44f2a26d35

See more details on using hashes here.

Supported by

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