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.5.tar.gz (20.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.5-py3-none-any.whl (36.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_inputfilter-0.0.5.tar.gz
  • Upload date:
  • Size: 20.1 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.5.tar.gz
Algorithm Hash digest
SHA256 238fa58cf1300fd3d26afd838ce4b84f1346c2adc65944e68af5a5efbf9140a6
MD5 f0928a966de1bc300c57d10cb57161d0
BLAKE2b-256 e91f6456ac4d6438cc60a37394c3d2a2ef8c04956bf0e4c804384478bdf69789

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_inputfilter-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8eab8636a53cd9ad72da8ae21fd9eb3719d16747c3a1d4a8fa17c761f65c8b5f
MD5 e84c43e58c0c98abe688a89fdde019e5
BLAKE2b-256 6bfad74d7246c0e5b42724b9e6aca24010d7e5d2277b40a0ae900e4be6d2318b

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