Skip to main content

A library to filter and validate input data inFlask applications

Project description

from src.flask_inputfilter.Validator import IsStringValidator

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, but it is also possible to create your own.

Definition

from flask_inputfilter import InputFilter
from flask_inputfilter.Condition import ExactlyOneOfCondition
from flask_inputfilter.Enum import RegexEnum
from flask_inputfilter.Filter import StringTrimFilter, ToIntegerFilter, ToNullFilter
from flask_inputfilter.Validator import IsIntegerValidator, IsStringValidator, RegexValidator


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

        super().__init__()

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

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

        self.add(
            'city',
            filters=[StringTrimFilter()],
            validators=[
                IsStringValidator()
            ]
        )

        self.addCondition(
            ExactlyOneOfCondition(['zipcode', 'city'])
        )

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.6.tar.gz (22.6 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.6-py3-none-any.whl (44.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_inputfilter-0.0.6.tar.gz
  • Upload date:
  • Size: 22.6 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.6.tar.gz
Algorithm Hash digest
SHA256 954ba942bb0f272ae686c971c7ed137a1d5e69c0c7b70bc4ca467730a90e3190
MD5 9fb69709800cc201e0f0690b3f067856
BLAKE2b-256 95b0f3176e19e94ead96b07931bd53d8c6af8cf287a5b592fd7759658aa1ac3a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_inputfilter-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 eee234fb3d1b0b5fb450a4b249b6b1f717f6288ad434914ba6043b4ad6da34d3
MD5 2b72f7dafd4d34c8cb73b2c826d4159c
BLAKE2b-256 9f048c7310b5d5e46ae7e9aaeb361ade517c57b3e343943928949717cb2d3129

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