Skip to main content

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

MIT

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.3.tar.gz (10.3 kB view hashes)

Uploaded Source

Built Distribution

flask_guard-0.0.3-py3-none-any.whl (6.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page