Skip to main content

A library to filter and validate input data inFlask applications

Project description

flask-inputfilter

The InputFilter class is used to validate and filter input data in Flask applications. It provides a modular way to clean and ensure that incoming data meets expected format and type requirements before being processed.

Installation

pip install flask-inputfilter

Usage

To use the InputFilter class, you need to create a new class that inherits from it and define the fields you want to validate and filter. There are lots of different filters and validators available to use, and you can also create your own custom filters and validators.

Definition

from flask_inputfilter import InputFilter
from flask_inputfilter.Enum import RegexEnum
from flask_inputfilter.Filters import ToIntFilter, ToNullFilter, StringTrimFilter
from flask_inputfilter.Validators import RegexValidator

class UpdateZipcodeInputFilter(InputFilter):
    def __init__(self):

        super().__init__()

        self.add(
            'id',
            required=True,
            filters=[ToIntFilter(), ToNullFilter()]
        )

        self.add(
            'zipcode',
            required=True,
            filters=[StringTrimFilter()],
            validators=[
                RegexValidator(
                    RegexEnum.POSTAL_CODE.value,
                    'The email is not in the format of an email.'
                )
            ]
        )

Usage

To use the InputFilter class, you need to call the validate method on the class instance. After calling the validate method, the validated data will be available in the g.validatedData object in the wanted format. If the data is not valid, the validate method will return a 400 response with the error message.

from flask import Flask, g

app = Flask(__name__)

@app.route('/update-zipcode', methods=['POST'])
@UpdateZipcodeInputFilter.validate()
def updateZipcode():
    data = g.validatedData

    # Do something with validatedData
    id = data.get('id')
    zipcode = data.get('zipcode')

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-inputfilter-0.0.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

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

flask_inputfilter-0.0.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file flask-inputfilter-0.0.0.tar.gz.

File metadata

  • Download URL: flask-inputfilter-0.0.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.20

File hashes

Hashes for flask-inputfilter-0.0.0.tar.gz
Algorithm Hash digest
SHA256 be301e3c90b505d4555e2ff12076aafb5040891d250fd70cb76bd3e7a3ecc747
MD5 73725ededebc555bf4e7b94ef3f1386b
BLAKE2b-256 5efebce2ad97b838ff1d4f741489236c2e341b9ed74e027ce8b9f8f277c7c31a

See more details on using hashes here.

File details

Details for the file flask_inputfilter-0.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_inputfilter-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6be8cbb8ef2ef4943669bb3d3481479490fbcab4324015438a6835dc39f8f5ff
MD5 1c4dfde29c9df4cc127927acd116daaf
BLAKE2b-256 6dcc4a14060d15776f39b529efdd1805957543f65d925fcc812345120e76d417

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