Skip to main content

A library to filter and validate input data inFlask applications

Reason this release was yanked:

Broken import destroys use, please use 0.0.7.1

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.

:Test Status:

.. image:: https://img.shields.io/github/actions/workflow/status/LeanderCS/flask-inputfilter/test.yaml?branch=main&style=flat-square&label=Github%20Actions
    :target: https://github.com/LeanderCS/flask-inputfilter/actions
.. image:: https://img.shields.io/coveralls/LeanderCS/flask-inputfilter/main.svg?style=flat-square&label=Coverage
    :target: https://coveralls.io/r/LeanderCS/flask-inputfilter

:Version Info:

.. image:: https://img.shields.io/pypi/v/flask-inputfilter?style=flat-square&label=PyPI
    :target: https://pypi.org/project/flask-inputfilter/

:Compatibility:

.. image:: https://img.shields.io/pypi/pyversions/flask-inputfilter?style=flat-square&label=PyPI
    :target: https://pypi.org/project/flask-inputfilter/

:Downloads:

.. image:: https://img.shields.io/pypi/dm/flask-inputfilter?style=flat-square&label=PyPI
    :target: https://pypi.org/project/flask-inputfilter/

Installation

.. code-block:: bash

pip install flask-inputfilter

Quickstart

To use the InputFilter class, create a new class that inherits from it and define the fields you want to validate and filter.

There are numerous filters and validators available, but you can also create your own <CREATE_OWN.md>.

Definition

.. code-block:: python

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=[ToIntegerFilter(), 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, call the validate method on the class instance. After calling validate, the validated data will be available in g.validatedData. If the data is invalid, a 400 response with an error message will be returned.

.. code-block:: python

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 validated data
    id = data.get('id')
    zipcode = data.get('zipcode')

Options

The add method supports several options:

  • Required_
  • Filter <flask_inputfilter/Filter/README.md>_
  • Validator <flask_inputfilter/Validator/README.md>_
  • Default_
  • Fallback_
  • ExternalApi <EXTERNAL_API.md>_

Required

The required option specifies whether the field must be included in the input data. If the field is missing, a ValidationError will be raised with an appropriate error message.

Default

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

Fallback

The fallback option specifies a value to use if validation fails or required data is missing. Note that if the field is optional and absent, fallback will not apply; use default in such cases.

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_inputfilter-0.0.7.tar.gz
  • Upload date:
  • Size: 14.5 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.7.tar.gz
Algorithm Hash digest
SHA256 62bf83f66072f2324e9eea53397e493aac6901da7ee071991cd4795962f288b4
MD5 dc5f4a424c98d7cce729c006d266e845
BLAKE2b-256 65c028c7f14f08f9e66ece879edfdf964ba3ff42fc8e1b876dbacb654e03447e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_inputfilter-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 a607500cd397e96832ceff433330dd630a89430667677bc434bc04d01e4131ab
MD5 1579d8cdacf95cba43bfa7c288dbb7af
BLAKE2b-256 eb651a38cb9983ee6b601b779b20a571418eab453dca1823be4c52eb3f8351dd

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