|
| 1 | +# flask-inputfilter |
| 2 | + |
| 3 | + |
| 4 | +[](https://pypi.org/project/flask-inputfilter/) |
| 5 | +[](https://pypi.org/project/flask-inputfilter/) |
| 6 | +[](https://github.com/LeanderCS/flask-inputfilter/blob/main/LICENSE) |
| 7 | +[](https://github.com/LeanderCS/flask-inputfilter/actions) |
| 8 | +[](https://coveralls.io/r/LeanderCS/flask-inputfilter) |
| 9 | +[](https://pypi.org/project/flask-inputfilter/) |
| 10 | +[](https://pypi.org/project/flask-inputfilter/) |
| 11 | + |
| 12 | +[](https://sonarcloud.io/summary/new_code?id=LeanderCS_flask-inputfilter) |
| 13 | +[](https://sonarcloud.io/summary/new_code?id=LeanderCS_flask-inputfilter) |
| 14 | +[](https://sonarcloud.io/summary/new_code?id=LeanderCS_flask-inputfilter) |
| 15 | +[](https://sonarcloud.io/summary/new_code?id=LeanderCS_flask-inputfilter)<br/> |
| 16 | +[](https://sonarcloud.io/summary/new_code?id=LeanderCS_flask-inputfilter) |
| 17 | +[](https://sonarcloud.io/summary/new_code?id=LeanderCS_flask-inputfilter) |
| 18 | +[](https://sonarcloud.io/summary/new_code?id=LeanderCS_flask-inputfilter) |
| 19 | +[](https://sonarcloud.io/summary/new_code?id=LeanderCS_flask-inputfilter) |
| 20 | + |
| 21 | + |
| 22 | +## Description |
| 23 | + |
| 24 | +This library is a Flask extension that provides a simple way to validate and filter input data. |
| 25 | +It allows you to define a set of rules for each field in your input data, including filters and validators. |
| 26 | +The library also supports conditions that can be used to enforce complex validation rules. |
| 27 | +It is designed to be straightforward to use and flexible, allowing you to create custom filters and validators as needed. |
| 28 | + |
| 29 | + |
| 30 | +## Quickstart |
| 31 | + |
| 32 | +To use the `InputFilter` class, create a new class that inherits from it and define the |
| 33 | +fields you want to validate and filter. |
| 34 | + |
| 35 | +There are many filters and validators available, but you can also create your [own](https://leandercs.github.io/flask-inputfilter/guides/create_own.html). |
| 36 | + |
| 37 | + |
| 38 | +## Installation |
| 39 | + |
| 40 | +Using [pip](https://pip.pypa.io/en/stable/getting-started/): |
| 41 | +```bash |
| 42 | + pip install flask-inputfilter |
| 43 | +``` |
| 44 | + |
| 45 | +Using [UV](https://docs.astral.sh/uv/): |
| 46 | +```shell |
| 47 | + uv add flask-inputfilter |
| 48 | +``` |
| 49 | + |
| 50 | +Using [Poetry](https://python-poetry.org/docs/): |
| 51 | +```shell |
| 52 | + poetry add flask-inputfilter |
| 53 | +``` |
| 54 | + |
| 55 | +There are plenty of wheels for all major environments and all supported python versions. |
| 56 | +But if you happen to use an environment where there is no wheel prebuilt, you can use either |
| 57 | +the python implementation or you can install ``flask-inputfilter`` with the dependencies needed |
| 58 | +for compilation by appending ``[compile]`` to the installation commands above. |
| 59 | + |
| 60 | +_**Note:**_ If you do decide to recompile PQA binaries, you will need to install platform-specific `C/C++` build |
| 61 | +tools like [Visual Studio](https://visualstudio.microsoft.com/), [Xcode](https://developer.apple.com/xcode/) or |
| 62 | +[GNU Make](https://www.gnu.org/software/make/) _(non-exhaustive list)_. |
| 63 | + |
| 64 | +A more detailed guide can be found [in the docs](https://leandercs.github.io/flask-inputfilter/guides/compile.html). |
| 65 | + |
| 66 | + |
| 67 | +### Definition |
| 68 | + |
| 69 | +```python |
| 70 | +from flask_inputfilter import InputFilter |
| 71 | +from flask_inputfilter.conditions import ExactlyOneOfCondition |
| 72 | +from flask_inputfilter.enums import RegexEnum |
| 73 | +from flask_inputfilter.filters import StringTrimFilter, ToIntegerFilter, ToNullFilter |
| 74 | +from flask_inputfilter.validators import IsIntegerValidator, IsStringValidator, RegexValidator |
| 75 | + |
| 76 | +class UpdateZipcodeInputFilter(InputFilter): |
| 77 | + def __init__(self): |
| 78 | + super().__init__() |
| 79 | + |
| 80 | + self.add( |
| 81 | + 'id', |
| 82 | + required=True, |
| 83 | + filters=[ToIntegerFilter(), ToNullFilter()], |
| 84 | + validators=[ |
| 85 | + IsIntegerValidator() |
| 86 | + ] |
| 87 | + ) |
| 88 | + |
| 89 | + self.add( |
| 90 | + 'zipcode', |
| 91 | + filters=[StringTrimFilter()], |
| 92 | + validators=[ |
| 93 | + RegexValidator( |
| 94 | + RegexEnum.POSTAL_CODE.value, |
| 95 | + 'The zipcode is not in the correct format.' |
| 96 | + ) |
| 97 | + ] |
| 98 | + ) |
| 99 | + |
| 100 | + self.add( |
| 101 | + 'city', |
| 102 | + filters=[StringTrimFilter()], |
| 103 | + validators=[ |
| 104 | + IsStringValidator() |
| 105 | + ] |
| 106 | + ) |
| 107 | + |
| 108 | + self.add_condition( |
| 109 | + ExactlyOneOfCondition(['zipcode', 'city']) |
| 110 | + ) |
| 111 | +``` |
| 112 | + |
| 113 | + |
| 114 | +### Usage |
| 115 | + |
| 116 | +To use the `InputFilter` class, call the `validate` method on the class instance. |
| 117 | +After calling `validate`, the validated data will be available in `g.validated_data`. |
| 118 | +If the data is invalid, a 400 response with an error message will be returned. |
| 119 | + |
| 120 | +```python |
| 121 | +from flask import Flask, g |
| 122 | +from your-path import UpdateZipcodeInputFilter |
| 123 | + |
| 124 | +app = Flask(__name__) |
| 125 | + |
| 126 | +@app.route('/update-zipcode', methods=['POST']) |
| 127 | +@UpdateZipcodeInputFilter.validate() |
| 128 | +def updateZipcode(): |
| 129 | + data = g.validated_data |
| 130 | + |
| 131 | + # Do something with validated data |
| 132 | + id = data.get('id') |
| 133 | + zipcode = data.get('zipcode') |
| 134 | + city = data.get('city') |
| 135 | +``` |
| 136 | + |
| 137 | + |
| 138 | +## See also |
| 139 | + |
| 140 | +For further instructions please view the [documentary](https://leandercs.github.io/flask-inputfilter). |
| 141 | + |
| 142 | +For ideas, suggestions or questions, please open an issue on [GitHub](https://github.com/LeanderCS/flask-inputfilter). |
0 commit comments