|
| 1 | +flask-inputfilter |
| 2 | +================================== |
| 3 | + |
| 4 | +The `InputFilter` class is used to validate and filter input data in Flask applications. |
| 5 | +It provides a modular way to clean and ensure that incoming data meets expected format |
| 6 | +and type requirements before being processed. |
| 7 | + |
| 8 | +:Test Status: |
| 9 | + |
| 10 | + .. image:: https://img.shields.io/github/actions/workflow/status/LeanderCS/flask-inputfilter/test.yaml?branch=main&style=flat-square&label=Github%20Actions |
| 11 | + :target: https://github.com/LeanderCS/flask-inputfilter/actions |
| 12 | + .. image:: https://img.shields.io/coveralls/LeanderCS/flask-inputfilter/main.svg?style=flat-square&label=Coverage |
| 13 | + :target: https://coveralls.io/r/LeanderCS/flask-inputfilter |
| 14 | + |
| 15 | +:Version Info: |
| 16 | + |
| 17 | + .. image:: https://img.shields.io/pypi/v/flask-inputfilter?style=flat-square&label=PyPI |
| 18 | + :target: https://pypi.org/project/flask-inputfilter/ |
| 19 | + |
| 20 | +:Compatibility: |
| 21 | + |
| 22 | + .. image:: https://img.shields.io/pypi/pyversions/flask-inputfilter?style=flat-square&label=PyPI |
| 23 | + :target: https://pypi.org/project/flask-inputfilter/ |
| 24 | + |
| 25 | +:Downloads: |
| 26 | + |
| 27 | + .. image:: https://img.shields.io/pypi/dm/flask-inputfilter?style=flat-square&label=PyPI |
| 28 | + :target: https://pypi.org/project/flask-inputfilter/ |
| 29 | + |
| 30 | +Installation |
| 31 | +============ |
| 32 | + |
| 33 | +.. code-block:: bash |
| 34 | +
|
| 35 | + pip install flask-inputfilter |
| 36 | +
|
| 37 | +Quickstart |
| 38 | +========== |
| 39 | + |
| 40 | +To use the `InputFilter` class, create a new class that inherits from it and define the |
| 41 | +fields you want to validate and filter. |
| 42 | + |
| 43 | +There are numerous filters and validators available, but you can also create your own. |
| 44 | +Refer to the [CREATE_OWN.md](CREATE_OWN.md) file for guidance. |
| 45 | + |
| 46 | +Definition |
| 47 | +---------- |
| 48 | + |
| 49 | +.. code-block:: python |
| 50 | +
|
| 51 | + from flask_inputfilter import InputFilter |
| 52 | + from flask_inputfilter.Condition import ExactlyOneOfCondition |
| 53 | + from flask_inputfilter.Enum import RegexEnum |
| 54 | + from flask_inputfilter.Filter import StringTrimFilter, ToIntegerFilter, ToNullFilter |
| 55 | + from flask_inputfilter.Validator import IsIntegerValidator, IsStringValidator, RegexValidator |
| 56 | +
|
| 57 | + class UpdateZipcodeInputFilter(InputFilter): |
| 58 | + def __init__(self): |
| 59 | + super().__init__() |
| 60 | +
|
| 61 | + self.add( |
| 62 | + 'id', |
| 63 | + required=True, |
| 64 | + filters=[ToIntegerFilter(), ToNullFilter()], |
| 65 | + validators=[ |
| 66 | + IsIntegerValidator() |
| 67 | + ] |
| 68 | + ) |
| 69 | +
|
| 70 | + self.add( |
| 71 | + 'zipcode', |
| 72 | + filters=[StringTrimFilter()], |
| 73 | + validators=[ |
| 74 | + RegexValidator( |
| 75 | + RegexEnum.POSTAL_CODE.value, |
| 76 | + 'The zipcode is not in the correct format.' |
| 77 | + ) |
| 78 | + ] |
| 79 | + ) |
| 80 | +
|
| 81 | + self.add( |
| 82 | + 'city', |
| 83 | + filters=[StringTrimFilter()], |
| 84 | + validators=[ |
| 85 | + IsStringValidator() |
| 86 | + ] |
| 87 | + ) |
| 88 | +
|
| 89 | + self.addCondition( |
| 90 | + ExactlyOneOfCondition(['zipcode', 'city']) |
| 91 | + ) |
| 92 | +
|
| 93 | +Usage |
| 94 | +----- |
| 95 | + |
| 96 | +To use the `InputFilter` class, call the `validate` method on the class instance. |
| 97 | +After calling `validate`, the validated data will be available in `g.validatedData`. |
| 98 | +If the data is invalid, a 400 response with an error message will be returned. |
| 99 | + |
| 100 | +.. code-block:: python |
| 101 | +
|
| 102 | + from flask import Flask, g |
| 103 | + from your-path import UpdateZipcodeInputFilter |
| 104 | +
|
| 105 | + app = Flask(__name__) |
| 106 | +
|
| 107 | + @app.route('/update-zipcode', methods=['POST']) |
| 108 | + @UpdateZipcodeInputFilter.validate() |
| 109 | + def updateZipcode(): |
| 110 | + data = g.validatedData |
| 111 | +
|
| 112 | + # Do something with validated data |
| 113 | + id = data.get('id') |
| 114 | + zipcode = data.get('zipcode') |
| 115 | +
|
| 116 | +Options |
| 117 | +======= |
| 118 | + |
| 119 | +The `add` method supports several options: |
| 120 | + |
| 121 | +- `Required`_ |
| 122 | +- `Filter` (see `Filter` documentation in :file:`flask_inputfilter/Filter/README.md`) |
| 123 | +- `Validator` (see `Validator` documentation in :file:`flask_inputfilter/Validator/README.md`) |
| 124 | +- `Default`_ |
| 125 | +- `Fallback`_ |
| 126 | +- `ExternalApi` (see :file:`EXTERNAL_API.md`) |
| 127 | + |
| 128 | +Required |
| 129 | +-------- |
| 130 | + |
| 131 | +The `required` option specifies whether the field must be included in the input data. |
| 132 | +If the field is missing, a `ValidationError` will be raised with an appropriate error message. |
| 133 | + |
| 134 | +Default |
| 135 | +------- |
| 136 | + |
| 137 | +The `default` option allows you to specify a default value to use if the field is not |
| 138 | +present in the input data. |
| 139 | + |
| 140 | +Fallback |
| 141 | +-------- |
| 142 | + |
| 143 | +The `fallback` option specifies a value to use if validation fails or required data |
| 144 | +is missing. Note that if the field is optional and absent, `fallback` will not apply; |
| 145 | +use `default` in such cases. |
0 commit comments