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
Release history Release notifications | RSS feed
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
File details
Details for the file flask-jsonschema-validator-0.0.4.tar.gz
.
File metadata
- Download URL: flask-jsonschema-validator-0.0.4.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fe1ee91fa16c6bc3eccf53ec6aa60f19dc0c65501210910adb562179ce66c0d5 |
|
MD5 | dee6d099045025a84bf2b2e05934d165 |
|
BLAKE2b-256 | bdb035f9f26d6efb6171da6236c07e5cbf73b66467c8954e983389d4656cbffa |
File details
Details for the file flask_jsonschema_validator-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: flask_jsonschema_validator-0.0.4-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 767439d796b36000283be84318dd25fca79dc6562f4acd4c6221ff09d19bc776 |
|
MD5 | 1021bff7ed880ce04b5cdd2f5f702b97 |
|
BLAKE2b-256 | 03040dc5605ba099d5984791c5a7faf046285bd27eca0f13b2de20be42869800 |