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-guard

Create the keys

from FlaskGuard 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 the validation function

from FlaskGuard import  FlaskGuard

# Init FlaskRequestGuard
guard = FlaskRequestGuard("myapp")

# Keys from the code snippet above
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 size or less,
#  and at least 0 size 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)

For an example where it's used with Flask, check out FlaskGuard-example repo.

Run tests

Run this commad in FlaskGuard directory to run the tests

python -m unittest discover tests

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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_guard-0.0.8-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file flask-guard-0.0.8.tar.gz.

File metadata

  • Download URL: flask-guard-0.0.8.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for flask-guard-0.0.8.tar.gz
Algorithm Hash digest
SHA256 a2c6acd42b150516d1f92d78d5ef1ed84767ce6ddb4285e1e8cb7d1250ac2e22
MD5 939cab07043005f7a27ff7c925ad35ef
BLAKE2b-256 dd4549bb0644153444548472467209172c25da310ffb00fc845e88231cbb93ed

See more details on using hashes here.

File details

Details for the file flask_guard-0.0.8-py3-none-any.whl.

File metadata

  • Download URL: flask_guard-0.0.8-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for flask_guard-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 4b010d2f231918161a6c31bcd95d628e3baf7c2ba93b39f63b83e0ea843384c0
MD5 562b43cf778968678e408f8e40dbb89a
BLAKE2b-256 2a83487ea5455214d497dcccef5947f5d31dff839a49742c618c406a32e86873

See more details on using hashes here.

Supported by

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