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

Quickstart

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.Filter import StringTrimFilter, ToIntegerFilter, ToNullFilter
from flask_inputfilter.Validator import IsIntegerValidator, RegexValidator 


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

        super().__init__()

        self.add(
            'id',
            required=True,
            filters=[ToIntegerFilter(), ToNullFilter()],
            validators=[
                IsIntegerValidator()
            ]
        )

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

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
from your-path import UpdateZipcodeInputFilter

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')

Options

The add method takes the following options:

Required

The required option is used to specify if the field is required or not. If the field is required and not present in the input data, the validate method will return the ValidationError with an error message.

Default

The default option is used to specify a default value to use if the field is not present in the input data.

Fallback

The fallback option is used to specify a fallback value to use if something unexpected happens, for example if the field is required but no value where provides or the validation fails.

This means that if the field is not required and no value is present, the fallback value will not be used. In this case you have to use the default option.

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.4.tar.gz (16.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.4-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_inputfilter-0.0.4.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for flask_inputfilter-0.0.4.tar.gz
Algorithm Hash digest
SHA256 14cc44a958cbbb93997e6edd767d99847f1d0139ada6f6174f759cf0674a2d4e
MD5 64f61b5b91d0a6c130b7f204717ba5ba
BLAKE2b-256 1d46c1edd5521b45241df583615c1c58d8d4fedb8facc6e042c60096557e0057

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_inputfilter-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f635258d30d407925a396bb69a3059894a44aae77e227e2f14b9a1c87492af03
MD5 ac821fdd8d27cc9e1dc84903bb6cc3a9
BLAKE2b-256 148d7fa2d2a619c8ee7e709b8f70b4314b2a5af5b0ce2ce3c6d6dff6e2649b9f

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