Skip to content

Commit afa8731

Browse files
committed
1 | Create new filters and validators
1 parent ff1813f commit afa8731

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1078
-397
lines changed

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[flake8]
22
exclude = __init__.py, venv, *.md, .*
3+
max-line-length = 90

DEVELOPMENT.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### Build docker image
44
```bash
5-
docker build -t jtrfaker .
5+
docker build -t flask-inputfilter .
66
```
77

88
### Run docker container in interactive mode
@@ -11,15 +11,15 @@ docker compose up -d
1111
```
1212

1313
```bash
14-
docker exec -it jtrfaker bash
14+
docker exec -it flask-inputfilter /bin/bash
1515
```
1616

1717
### Run tests
1818
```bash
19-
docker exec -it jtrfaker pytest
19+
docker exec -it flask-inputfilter pytest
2020
```
2121

2222
### Run linting
2323
```bash
24-
docker exec -it jtrfaker flake8
24+
docker exec -it flask-inputfilter black .
2525
```

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ It provides a modular way to clean and ensure that incoming data meets expected
99
pip install flask-inputfilter
1010
```
1111

12-
## Usage
12+
## Quickstart
1313

1414
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.
1515
There are lots of different filters and validators available to use, and you can also create your own custom filters and validators.
@@ -19,8 +19,8 @@ There are lots of different filters and validators available to use, and you can
1919
```python
2020
from flask_inputfilter import InputFilter
2121
from flask_inputfilter.Enum import RegexEnum
22-
from flask_inputfilter.Filter import ToIntegerFilter, ToNullFilter, StringTrimFilter
23-
from flask_inputfilter.Validator import RegexValidator
22+
from flask_inputfilter.Filter import StringTrimFilter, ToIntegerFilter, ToNullFilter
23+
from flask_inputfilter.Validator import IsIntegerValidator, RegexValidator
2424

2525

2626
class UpdateZipcodeInputFilter(InputFilter):
@@ -31,7 +31,10 @@ class UpdateZipcodeInputFilter(InputFilter):
3131
self.add(
3232
'id',
3333
required=True,
34-
filters=[ToIntegerFilter(), ToNullFilter()]
34+
filters=[ToIntegerFilter(), ToNullFilter()],
35+
validators=[
36+
IsIntegerValidator()
37+
]
3538
)
3639

3740
self.add(
@@ -41,7 +44,7 @@ class UpdateZipcodeInputFilter(InputFilter):
4144
validators=[
4245
RegexValidator(
4346
RegexEnum.POSTAL_CODE.value,
44-
'The email is not in the format of an email.'
47+
'The zipcode is not in the correct format.'
4548
)
4649
]
4750
)
@@ -69,3 +72,25 @@ def updateZipcode():
6972
zipcode = data.get('zipcode')
7073
```
7174

75+
## Options
76+
77+
The `add` method takes the following options:
78+
79+
- [`Required`](#required)
80+
- [`Filter`](src/flask_inputfilter/Filter/README.md)
81+
- [`Validator`](src/flask_inputfilter/Validator/README.md)
82+
- [`Default`](#default)
83+
- [`Fallback`](#fallback)
84+
85+
### Required
86+
87+
The `required` option is used to specify if the field is required or not.
88+
If the field is required and not present in the input data, the `validate` method will return a 400 response with the error message.
89+
90+
### Default
91+
92+
The `default` option is used to specify a default value to use if the field is not present in the input data.
93+
94+
### Fallback
95+
96+
The `fallback` option is used to specify a fallback value to use if the field is not present in the input data, although it is required or the validation fails.

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[build-system]
22
requires = ["setuptools", "wheel"]
33
build-backend = "setuptools.build_meta"
4+
5+
[tool.black]
6+
line-length = 90

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pillow==2.0.0
44
pytest
55
setuptools
66
twine
7+
black

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
version="0.0.3",
66
author="Leander Cain Slotosch",
77
author_email="[email protected]",
8-
description="A library to filter and validate input data in"
9-
"Flask applications",
8+
description="A library to filter and validate input data in" "Flask applications",
109
long_description=open("README.md").read(),
1110
long_description_content_type="text/markdown",
1211
url="https://github.com/LeanderCS/flask-inputfilter",

src/flask_inputfilter/Enum/RegexEnum.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@ class RegexEnum(Enum):
66
Enum for regex patterns.
77
"""
88

9-
EMAIL = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
9+
EMAIL = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
1010

11-
IPV4_ADDRESS = r'^(?:\d{1,3}\.){3}\d{1,3}$'
12-
IPV6_ADDRESS = r'^\[?([a-fA-F0-9:]+:+)+[a-fA-F0-9]+\]?$'
11+
IPV4_ADDRESS = r"^(?:\d{1,3}\.){3}\d{1,3}$"
12+
IPV6_ADDRESS = r"^\[?([a-fA-F0-9:]+:+)+[a-fA-F0-9]+\]?$"
1313

14-
ISO_DATE = r'^\d{4}-\d{2}-\d{2}$'
15-
ISO_DATETIME = (r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}'
16-
r'(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?$')
14+
ISO_DATE = r"^\d{4}-\d{2}-\d{2}$"
15+
ISO_DATETIME = (
16+
r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}" r"(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?$"
17+
)
1718

18-
PHONE_NUMBER = r'^\+?[\d\s\-()]{7,}$'
19+
PHONE_NUMBER = r"^\+?[\d\s\-()]{7,}$"
1920

20-
POSTAL_CODE = r'^\d{4,10}$'
21+
POSTAL_CODE = r"^\d{4,10}$"
2122

22-
URL = r'^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$'
23+
URL = r"^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$"

src/flask_inputfilter/Exception/ValidationError.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
class ValidationError(Exception):
32
"""
43
This class is used to raise an exception when a validation error occurs.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import Any, List, Optional
2+
3+
from ..Filter.BaseFilter import BaseFilter
4+
5+
6+
class ArrayExplodeFilter(BaseFilter):
7+
"""
8+
Filter that splits a string into an array based on a specified delimiter.
9+
"""
10+
11+
def __init__(self, delimiter: str = ",") -> None:
12+
13+
self.delimiter = delimiter
14+
15+
def apply(self, value: Any) -> Optional[List[str]]:
16+
17+
if not isinstance(value, str):
18+
return None
19+
20+
return value.split(self.delimiter)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Filter
2+
3+
The `Filter` module contains the filters that can be used to filter the input data.
4+
5+
## Available filters
6+
7+
The following filters are available in the `Filter` module:
8+
9+
1. [`ArrayExplodeFilter`](src/flask_inputfilter/Filter/ArrayExplodeFilter.py) - Explodes the input string into an array.
10+
2. [`StringTrimFilter`](src/flask_inputfilter/Filter/StringTrimFilter.py) - Trims the whitespace from the beginning and end of the string.
11+
3. [`ToBooleanFilter`](src/flask_inputfilter/Filter/ToBooleanFilter.py) - Converts the string to a boolean value.
12+
4. [`ToCamelCaseFilter`](src/flask_inputfilter/Filter/ToCamelCaseFilter.py) - Converts the string to camel case.
13+
5. [`ToFloatFilter`](src/flask_inputfilter/Filter/ToFloatFilter.py) - Converts the string to a float value.
14+
5. [`ToIntegerFilter`](src/flask_inputfilter/Filter/ToIntegerFilter.py) - Converts the string to an integer value.
15+
6. [`ToLowerFilter`](src/flask_inputfilter/Filter/ToLowerFilter.py) - Converts the string to lowercase.
16+
7. [`ToNullFilter`](src/flask_inputfilter/Filter/ToNullFilter.py) - Converts the string to `None` if it is already `None` or `''` (empty string).
17+
8. [`ToPascaleCaseFilter`](src/flask_inputfilter/Filter/ToPascaleCaseFilter.py) - Converts the string to pascal case.
18+
9. [`ToSnakeCaseFilter`](src/flask_inputfilter/Filter/ToSnakeCaseFilter.py) - Converts the string to snake case.
19+
9. [`ToStringFilter`](src/flask_inputfilter/Filter/ToStringFilter.py) - Converts the input to a string value.
20+
9. [`ToUpperFilter`](src/flask_inputfilter/Filter/ToUpperFilter.py) - Converts the string to uppercase.
21+
10. [`WhitespaceCollapseFilter`](src/flask_inputfilter/Filter/WhitespaceCollapseFilter.py) - Collapses the whitespace in the string.

0 commit comments

Comments
 (0)