Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sanjeevan/flask-json-schema
Flask extension to validate JSON requests using the jsonschema spec
https://github.com/sanjeevan/flask-json-schema
flask jsonschema
Last synced: 2 months ago
JSON representation
Flask extension to validate JSON requests using the jsonschema spec
- Host: GitHub
- URL: https://github.com/sanjeevan/flask-json-schema
- Owner: sanjeevan
- License: mit
- Created: 2018-05-25T12:47:21.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-07-05T13:38:55.000Z (over 6 years ago)
- Last Synced: 2024-10-01T05:01:23.360Z (3 months ago)
- Topics: flask, jsonschema
- Language: Python
- Homepage:
- Size: 9.77 KB
- Stars: 11
- Watchers: 3
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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:
```python
from flask import Flask
from flask_json_schema import JsonSchemaapp = Flask(__name__)
schema = JsonSchema(app)
```Or through the factory method:
```python
schema = JsonSchema()app = Flask(__name__)
schema.init_app(app)
```## Quick example
```python
from flask_json_schema import JsonSchema, JsonValidationError
from flask import Flask, jsonify, requestapp = 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
* [Source Code](https://github.com/sanjeevan/flask-json-schema)
* [Issues](https://github.com/sanjeevan/flask-json-schema/issues)