This framework is designed to validate request params for Flask web framework.
It has the following unique features:
- Cross Field and Cross Struct validations
- Array and Map diving, which allows any or all levels of a multidimensional field to be validated
- Customizable error response
you can simply install it by PyPi:
pip install pre-request
pre-request manual could be found at: https://pre-request.readthedocs.io/en/master/index.html
This is very easy to use pre-request in your project
from flask import Flask
from pre_request import pre, Rule
app = Flask(__name__)
args = {
"userId": Rule(type=int, required=True)
}
@app.route("/")
@pre.catch(args)
def hello_world(params):
from flask import g
return str(params == g.params)
what happened in this code ?
- Use pre-request library to import a global object pre
- Define request params rule, e.g. userId must be type of int and required
- Use @pre.catch(req_params) to filter input value
- Use ~flask.g or def hello_world(params) to get formatted input value。
We use a very complex example to show the powerful of this framework. Otherwise you can find amount of examples at: https://github.com/Eastwu5788/pre-request/tree/master/examples
from flask import Flask
from pre_request import pre, Rule
args = {
"userFirst": {
"userId": Rule(type=int, required=False),
"socialInfo": {
"gender": Rule(type=int, enum=[1, 2], default=1),
"age": Rule(type=int, gte=18, lt=80),
"country": Rule(required=True, deep=False)
}
},
"userSecond": {
"userId": Rule(type=int, required=False, neq_key="userFirst.userId"),
"friends": Rule(type=dict, required=True, multi=True, structure={
"userId": Rule(type=int, required=True, dest="user_id"),
"userName": Rule(type=str, required=True, dest="user_name")
})
}
}
app = Flask(__name__)
app.config["TESTING"] = True
client = app.test_client()
@app.route("/structure", methods=["GET", "POST"])
@pre.catch(args)
def structure_handler(params):
return str(params)
if __name__ == "__main__":
resp = app.test_client().post("/structure", json={
"userFirst": {
"userId": "13",
"socialInfo": {
"age": 20,
}
},
"userSecond": {
"userId": 14,
"friends": [
{
"userId": 13,
"userName": "Trump"
}
]
},
"country": "CN",
"userFirst.socialInfo.gender": 1
})
print(resp.get_data(as_text=True))
We can use function pre.parse instead of decorator @pre.catch()
args = {
"params": Rule(email=True)
}
@app.errorhandler(ParamsValueError)
def params_value_error(e):
return pre.fmt_resp(e)
@app.route("/index")
def example_handler():
rst = pre.parse(args)
return str(rst)
How to make a contribution to Pre-request, see the contributing.
Please give me a cup of coffee, thank you!
BTC: 1657DRJUyfMyz41pdJfpeoNpz23ghMLVM3
ETH: 0xb098600a9a4572a4894dce31471c46f1f290b087
- Documentaion: https://pre-request.readthedocs.io/en/master/index.html
- Release: https://pypi.org/project/pre-request/
- Code: https://github.com/Eastwu5788/pre-request
- Issue tracker: https://github.com/Eastwu5788/pre-request/issues
- Test status: https://coveralls.io/github/Eastwu5788/pre-request