Skip to main content

Flask Parameter Validation

Get and validate all Flask input parameters with ease.

Install

  • Pip: Install with pip install flask_parameter_validation.
  • Manually:
    • git clone https://github.com/Ge0rg3/Flask-Parameter-Validation.git
    • python setup.py install

Simple Usage

from flask import Flask
from typing import List, Optional
from flask_parameter_validation import ValidateParameters, Route, Json, Query

app = Flask(__name__)


@app.route("/update/<int:id>", methods=["POST"])
@ValidateParameters()
def hello(
        id: int = Route(),
        username: str = Json(min_length=5, blacklist="<>"),
        age: int = Json(min_int=18, max_int=99),
        nicknames: List[str] = Json(),
        password_expiry: Optional[int] = Json(5),
        is_admin: bool = Query(False)
     ):
    return "Hello World!"


if __name__ == "__main__":
    app.run()

Detailed Usage

  1. We use the ValidateParameters decorator on all functions that this modules should be used in.
  2. The format for arguments is as follows: parameter_name: parameter_type = Class() In this example, parameter_name would be the field name itself, such as "username". parameter_type would be the expected python data type, such as str, int, List, Union etc. Finally, Class() is one of the class inputs, as detailed below:

Classes

  1. Route()
    This is the data passed through the URL, such as /users/<int:id>
  2. Form()
    This is the data passed by a normal HTML form, often with x-www-form-urlencoded content-type.
  3. Json()
    This is any JSON body sent -- request must have application/json content type for flask to read this.
  4. Query()
    This covers query parameters (aka GET parameters), such as /news/article?id=55
  5. File()
    The validation on files are different to the others, but file input can still be obtained here as their Flask FileStorage objects.

Validation

All parameters can have default values, and automatic validation.
Route, Form, Json and Query have the following options:

  • default: any, Specifies the default value for the field.
  • min_length: int, Specifies the minimum character length for a string input
  • max_length: int, Specifies the maximum character length for a string input
  • min_list_length: int, Specifies the minimum number of elements in a list
  • max_list_length: int, Specifies the maximum number of elements in a list
  • min_int: int, Specifies the minimum number for an int input
  • max_int: int, Specifies the maximum number for an int input
  • whitelist: str, A string containing allowed characters for the value
  • blacklist: str, A string containing forbidden characters for the value

File has the following options:

  • content_types: array of strings, an array of allowed content types.
  • min_length: Minimum content-length for a file
  • max_length: Maximum content-length for a file

These validators are passed into the classes in the route function, such as:

  • username: str = Json("defaultusername", min_length=5)
  • profile_picture: Any = File(content_types=["image/png", "image/jpeg"])
  • filter: str = Query()

Overwriting default errors

By default, the error messages are returned as a JSON response, with the detailed error in the "error" field. However, this can be edited by passing a custom error function into the ValidateParameters decorator. For example:

def my_error_func(error_message):
    return f"This is the error! Please sort it out! {error_message}", 400


@ValidateParameters(my_error_func)
def api(...)

Metadata

Release files for Flask-Parameter-Validation 1.0.23

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-Parameter-Validation 1.0.23
File Size Uploaded
Flask-Parameter-Validation-1.0.23.tar.gz 6.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Flask-Parameter-Validation 1.0.23
File Interpreter ABI Platform
Flask_Parameter_Validation-1.0.23-py3-none-any.whl Python 3 none any Details

Total release size: 18.8 kB

Release files / Flask-Parameter-Validation-1.0.23.tar.gz

Download URL Flask-Parameter-Validation-1.0.23.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
eb3649c5d1816a063a8e56c064ca4c554ce697845210fe4170a6ca755166e4d7
BLAKE2b-256 checksum
How to use checksums
3d170ebf9a85f412318f3acb53137275b123590cef747db05a758414660d8205
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Release files / Flask_Parameter_Validation-1.0.23-py3-none-any.whl

Download URL Flask_Parameter_Validation-1.0.23-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4050c45adff213a7a5547f6dc8bac3ff543fd67f76bad294a474d3d502058aa9
BLAKE2b-256 checksum
How to use checksums
84ff5586902b03f6fd62808d21a1aebf9d5385f74ac234ef0b88f28050c4c38a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Release history Release notifications | RSS feed

2.6.0

2 release files

2.5.0

2 release files

2.4.0

3 release files

2.3.1

2 release files

2.3.0

2 release files

2.2.4

2 release files

2.2.3

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.9

2 release files

2.1.8

2 release files

2.1.7

2 release files

2.1.6

2 release files

2.1.5

2 release files

2.1.4

2 release files

2.1.3

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.6

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.24

2 release files

This release

1.0.23 This release

2 release files

1.0.22

2 release files

1.0.21

1 release file

1.0.20

1 release file

1.0.18

1 release file

1.0.17

1 release file

1.0.16

1 release file

1.0.15

1 release file

1.0.14

1 release file

1.0.2

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page