A library for checking if JSON requests have valid data.
Project description
FlaskRequestGuard
Flask Request Guard is a Python library for validating incoming HTTP requests in Flask web applications. It provides a simple yet flexible way to define request rules and enforce them automatically on incoming requests, ensuring that only requests that meet the specified requirements are allowed to proceed.
Installation
Use the package manager pip to intall flask-request-gaurd.
pip install flask-request-gaurd
Creat the keys
from FlaskRequestGuard import RequestParameter
# Creates a key that has the name "name" and type str with a minimum length of 0 and a maximum length of 10
name_key = RequestParameter("name", str, 0, 10)
# Creates a key that has the name "range" and type int with a minimum value of -10 and a maximum value of 10
range_key = RequestParameter("range", int, -10, 10)
Create validate function
from FlaskRequestGuard import FlaskRequestGuard
# Init FlaskRequestGuard
guard = FlaskRequestGuard("myapp")
required_keys = [range_key, name_key]
# Returns a function that is used to check a request
validate_user_request = guard.create_validate_function(required_keys)
Check request
request = {"name": "erik", "age": 23, "range": 2}
# Returns True, {"error_messages": []}
is_valid, error_messages = validate_user_request(request)
request = {"name": "eeeeeeeeeee", "age": 23, "range": 2}
# Returns False, {"error_messages": ["The 'name' field must be 10 characters or less,
# and at least 0 characters or more, but is actually 11 characters long."]}
is_valid, error_messages = validate_user_request(request)
request = {"age": 23, "range": 2}
# Returns False, {"error_messages": ["Missing name field in request body."]}
is_valid, error_messages = validate_user_request(request)
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
flask-guard-0.0.4.tar.gz
(10.2 kB
view hashes)
Built Distribution
Close
Hashes for flask_guard-0.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67f5e0804698469e426f627c5bdfdbe6e3c6ca7534c9333f929ff28e2b245dac |
|
MD5 | 0575e67c8465cd823fa92cee22fab69e |
|
BLAKE2b-256 | b5c77cfa2d43519677ae93c21147eb1ffd039239a79037d39eb0f21f6fce1bfa |