No project description provided
Project description
flask-json-pattern
Decorator for REST endpoints in flask. Validate JSON request data.
Installation
Use pip to install the package:
pip install flask-json-pattern
Usage
This package provides a flask route decorator to validate json payload.
from flask import Flask, jsonify
from flask_json_pattern import json_pattern
app = Flask(__name__)
@app.route('/greet', methods=['POST'])
@json_pattern({
'name': {'type': str},
'surname': {'type': str}
})
def greet():
return jsonify({"msg": "Hello %(name)s %(surname)s" % request.json})
Required fields
By default all fields are required, If you want you can change this behavior by adding required: False
in the field you want
@app.route('/greet', methods=['POST'])
@json_pattern({
'name': {'type': str},
'surname': {'type': str, 'required': False}
})
def greet():
return ""
Default values
When the field is optional required: False
, you could set the default value of it using the default
property.
@app.route('/greet', methods=['POST'])
@json_pattern({
'name': {'type': str},
'surname': {'type': str, "default": 'unknown'}
})
def greet():
return ""
Empty values
By default the fields can be empty, you could change this if you set the empty
property to false.
@app.route('/greet', methods=['POST'])
@json_pattern({
'name': {'type': str},
'surname': {'type': str, "empty": False}
})
def greet():
return ""
Also, you could change the empty and default properties, so that when the property is empty assign a value.
@app.route('/greet', methods=['POST'])
@json_pattern({
'name': {'type': str, "empty": False, 'default': "unknown"},
'surname': {'type': str}
})
def greet():
return ""
Skip validation methods
If you want to skip the validation for some HTTP methods, can you set ignore_methods=[]
. By default methods that do not expect a body are GET, HEAD and DELETE.
@app.route('/greet', methods=['GET', 'POST'])
@json_pattern({
'name': {'type': str},
'surname': {'type': str}
}, ignore_methods=['GET'])
def greet():
return ""
Tuple and list
If you want, you can validate the data type of each position of a simple list
@app.route('/greet', methods=['GET', 'POST'])
@json_pattern({
'name': {'type': str},
'surname': {'type': str},
'skills': {
'type': list,
'of': str
}
}, ignore_methods=['GET'])
def greet():
return ""
Also, If you want you could validate lists of objects, and the fields of each object.
@app.route('/greet', methods=['GET', 'POST'])
@json_pattern({
'name': {'type': str},
'surname': {'type': str},
'skills': {
'type': list,
"schema": {
"name": {"type": str}
}
}
}, ignore_methods=['GET'])
def greet():
return ""
Error handling
On validation failure the library calls flask.abort
and passes an 400 error code and instance of the ValidationError.
from flask import make_response, jsonify
from flask_json_pattern import ValidationError
@app.errorhandler(400)
def bad_request(error):
if isinstance(error.description, ValidationError):
return make_response(jsonify({'msg': error.description.message}), 400)
return error
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
File details
Details for the file flask_json_pattern-0.0.2.tar.gz
.
File metadata
- Download URL: flask_json_pattern-0.0.2.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7763b5cf959912f1eeeed17fd26196f6f1ccab1bb34129778e42909b499e8238 |
|
MD5 | 690950e43c681a65cc09ad0ab2d54068 |
|
BLAKE2b-256 | 7d3e103feb8d0c81268b81ca3cf4dc09cefbc64ff636fb417fe43eba4f86d860 |