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.1.tar.gz (10.1 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.1-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file flask_inputfilter-0.0.1.tar.gz.

File metadata

  • Download URL: flask_inputfilter-0.0.1.tar.gz
  • Upload date:
  • Size: 10.1 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.1.tar.gz
Algorithm Hash digest
SHA256 6df7f16de4f5266f5391223f068bd6dd5d8b8c343ad6503ebf5fbb58cb0b6ba8
MD5 8127e15fdc05a6e536f01c04056ee721
BLAKE2b-256 3714b493a8f51dc315513bfa3ab4d42bf6322ed8d1642c9573cfead4a6a9fb5f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_inputfilter-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6d80874af5daae209e1c4c76ceed63006d06c001fa8ffab0dc0c1ecff54bb7c9
MD5 91fb284c94d187c9cc7680b6be62af88
BLAKE2b-256 e24b6ea4f1ce1f67b75b81e3135c9389485d38a885992bb875d4579ae59a4883

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