Skip to main content

A library to filter and validate input data in Flask 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.

Thank you for using flask-inputfilter!

If you have any questions or suggestions, please feel free to open an issue on GitHub.

If you don’t want to miss any updates, please star the repository. This will help me to understand how many people are interested in this project.

For information about the usage you can view the documentation.

Test Status:
https://img.shields.io/github/actions/workflow/status/LeanderCS/flask-inputfilter/test.yaml?branch=main&style=flat-square&label=Github%20Actions https://img.shields.io/coveralls/LeanderCS/flask-inputfilter/main.svg?style=flat-square&label=Coverage
Version Info:
https://img.shields.io/pypi/v/flask-inputfilter?style=flat-square&label=PyPI
Compatibility:
https://img.shields.io/pypi/pyversions/flask-inputfilter?style=flat-square&label=PyPI
Downloads:
https://static.pepy.tech/badge/flask-inputfilter/month https://static.pepy.tech/badge/flask-inputfilter

Installation

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.

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=[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.validated_data. If the data is invalid, a 400 response with an error message will be returned.

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.validated_data

    # Do something with validated data
    id = data.get('id')
    zipcode = data.get('zipcode')
    city = data.get('city')

See also

For further instructions please view the documentary.

For ideas, suggestions or questions, please open an issue on GitHub.

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: flask_inputfilter-0.1.1.tar.gz
  • Upload date:
  • Size: 40.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for flask_inputfilter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c07693147ff4de02863bd61837de7e0b3d1e29c4b9f8901fc00890e70fda62bb
MD5 f631f31e4232148d7c6906ffe0ce9ed9
BLAKE2b-256 7d813f10670410350dd9bbef4c6562f61d90139b7c28f93d37d840f9d2a5e681

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_inputfilter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8683a2f118fca0bb00f5ec30629e77117535d7f403190cf0f210ae6bc10ef78e
MD5 9b5274e1e240cfdd7f7b870147be462c
BLAKE2b-256 29937da5e2fe9a989d4f50ef4f02a634a04a9df73225ed7fd60c38c77581ec93

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