Flask-json-schema
This extension makes it easy to validate JSON data that is sent to your Flask app using the jsonschema spec
Setup
Flask-json-schema is available on PyPI and can be installed with
pip install flask-json-schema
The extension can either be initialized directly:
from flask import Flask
from flask_json_schema import JsonSchema
app = Flask(__name__)
schema = JsonSchema(app)
Or through the factory method:
schema = JsonSchema()
app = Flask(__name__)
schema.init_app(app)
Quick example
from flask_json_schema import JsonSchema, JsonValidationError
from flask import Flask, jsonify, request
app = Flask(__name__)
schema = JsonSchema(app)
todo_schema = {
'required': ['todo'],
'properties': {
'todo': { 'type': 'string' },
'priority': { 'type': 'integer' },
}
}
todos = []
@app.errorhandler(JsonValidationError)
def validation_error(e):
return jsonify({ 'error': e.message, 'errors': [validation_error.message for validation_error in e.errors]})
@app.route('/todo', methods=['GET', 'POST'])
@schema.validate(todo_schema)
def create_message():
if request.method == 'POST':
todos.append( request.get_json() )
return jsonify({ 'success': True, 'message': 'Created todo' })
return jsonify(todos)
app.run('0.0.0.0', 5000, debug=True)
See example.py for the source code
Links
Metadata
Release files for flask-json-schema 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| Flask-json-schema-0.0.5.tar.gz | 2.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| Flask_json_schema-0.0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 5.6 kB
Release files / Flask-json-schema-0.0.5.tar.gz
| Download URL | Flask-json-schema-0.0.5.tar.gz |
|---|---|
| Size | 2.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b6f9f7c4d0be644ab6e5a780733edb2e09c132c09a4f64756e3d45c19e35c967
|
|
BLAKE2b-256 checksum How to use checksums |
5fbf93a452f97f9369e0f5288f3566aef0f66f7420cccf509ba5c1ea401d283a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / Flask_json_schema-0.0.5-py3-none-any.whl
| Download URL | Flask_json_schema-0.0.5-py3-none-any.whl |
|---|---|
| Size | 2.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e0e19fcd9092fac51e70a5f5f59de24837a821a89cb7323d702c46846202bea0
|
|
BLAKE2b-256 checksum How to use checksums |
0f02a901d29b5b6f48e5cff98620d012c32272f9cb65c793dc0fd67a1335eff6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |