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 a 400 response with the 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 the field is not present in the input data, although it is required or the validation fails.

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.3.tar.gz (13.0 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.3-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_inputfilter-0.0.3.tar.gz
  • Upload date:
  • Size: 13.0 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.3.tar.gz
Algorithm Hash digest
SHA256 088c3d92e085bf9ca429169f6cb0a6c1b5f3553a4a1e4ef58029e25fbfd6f306
MD5 3850fa0b96beb4c6a65978aa1993160a
BLAKE2b-256 3cbf3d658e78f5c2f69784d35939330169eee1fae910e47d05efe96ec911af0a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_inputfilter-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c2bf6172d5f637cedcdb158569d79544a2a17870734da5d49017ac27b4c4c208
MD5 7df2ff8fb16c6beac7cd1bba91a7ea4f
BLAKE2b-256 f0efd821034f0f90594929427ade60342a3db0130d6157d0dec09e18b564e655

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