Skip to content

Commit 28fb9f5

Browse files
authored
Merge pull request #7 from LeanderCS/6
6 | Add field dependency functionality
2 parents 60d5aa8 + 9da79ab commit 28fb9f5

File tree

75 files changed

+1091
-135
lines changed

Some content is hidden

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

75 files changed

+1091
-135
lines changed

.flake8

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

CHAGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
## [0.0.5] - 2025-01-12
6+
7+
### Added
8+
9+
- New condition functionality between fields. [Check it out](src/flask_inputfilter/Condition/README.md)
10+
11+
### Changed
12+
13+
- Switched external_api config from dict to class. [Check it out](src/flask_inputfilter/Model/ExternalApiConfig.py)

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 Leander Cain Slotosch
3+
Copyright (c) 2025 Leander Cain Slotosch
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from src.flask_inputfilter.Validator import IsStringValidator
2+
13
# flask-inputfilter
24

35
The `InputFilter` class is used to validate and filter input data in Flask applications.
@@ -16,15 +18,18 @@ pip install flask-inputfilter
1618
## Quickstart
1719

1820
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.
19-
There are lots of different filters and validators available to use, and you can also create your own custom filters and validators.
21+
22+
There are lots of different filters and validators available to use, but it is also possible to create your own.
2023

2124
### Definition
2225

2326
```python
27+
2428
from flask_inputfilter import InputFilter
29+
from flask_inputfilter.Condition import ExactlyOneOfCondition
2530
from flask_inputfilter.Enum import RegexEnum
2631
from flask_inputfilter.Filter import StringTrimFilter, ToIntegerFilter, ToNullFilter
27-
from flask_inputfilter.Validator import IsIntegerValidator, RegexValidator
32+
from flask_inputfilter.Validator import IsIntegerValidator, IsStringValidator, RegexValidator
2833

2934

3035
class UpdateZipcodeInputFilter(InputFilter):
@@ -35,15 +40,14 @@ class UpdateZipcodeInputFilter(InputFilter):
3540
self.add(
3641
'id',
3742
required=True,
38-
filters=[ToIntegerFilter(), ToNullFilter()],
43+
filters=[ToNullFilter()],
3944
validators=[
4045
IsIntegerValidator()
4146
]
4247
)
4348

4449
self.add(
4550
'zipcode',
46-
required=True,
4751
filters=[StringTrimFilter()],
4852
validators=[
4953
RegexValidator(
@@ -52,6 +56,19 @@ class UpdateZipcodeInputFilter(InputFilter):
5256
)
5357
]
5458
)
59+
60+
self.add(
61+
'city',
62+
filters=[StringTrimFilter()],
63+
validators=[
64+
IsStringValidator()
65+
]
66+
)
67+
68+
self.addCondition(
69+
ExactlyOneOfCondition(['zipcode', 'city'])
70+
)
71+
5572
```
5673

5774
### Usage
@@ -61,6 +78,7 @@ After calling the `validate` method, the validated data will be available in the
6178
If the data is not valid, the `validate` method will return a 400 response with the error message.
6279

6380
```python
81+
6482
from flask import Flask, g
6583
from your-path import UpdateZipcodeInputFilter
6684

@@ -74,6 +92,7 @@ def updateZipcode():
7492
# Do something with validatedData
7593
id = data.get('id')
7694
zipcode = data.get('zipcode')
95+
7796
```
7897

7998
---

docker-compose.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
services:
2-
app:
2+
3+
flask-inputfilter:
34
build:
45
context: .
56
dockerfile: Dockerfile

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="flask_inputfilter",
5-
version="0.0.4",
5+
version="0.0.5",
66
author="Leander Cain Slotosch",
77
author_email="[email protected]",
88
description="A library to filter and validate input data in"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import Any, Dict
2+
3+
from .BaseCondition import BaseCondition
4+
5+
6+
class ArrayLengthEqualCondition(BaseCondition):
7+
"""
8+
Condition that checks if the array is of the specified length.
9+
"""
10+
11+
def __init__(self, first_field: str, second_field: str) -> None:
12+
13+
self.first_field = first_field
14+
self.second_field = second_field
15+
16+
def check(self, data: Dict[str, Any]) -> bool:
17+
18+
return len(data.get(self.first_field) or []) == len(
19+
data.get(self.second_field) or []
20+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import Any, Dict
2+
3+
from .BaseCondition import BaseCondition
4+
5+
6+
class ArrayLongerThanCondition(BaseCondition):
7+
"""
8+
Condition that checks if the array is longer than the specified length.
9+
"""
10+
11+
def __init__(self, longer_field: str, shorter_field: str) -> None:
12+
13+
self.longer_field = longer_field
14+
self.shorter_field = shorter_field
15+
16+
def check(self, data: Dict[str, Any]) -> bool:
17+
18+
return len(data.get(self.longer_field) or []) > len(
19+
data.get(self.shorter_field) or []
20+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from typing import Any, Dict
2+
3+
4+
class BaseCondition:
5+
"""
6+
Base class for defining conditions.
7+
Each condition should implement the `check` method.
8+
"""
9+
10+
def check(self, data: Dict[str, Any]) -> bool:
11+
12+
raise NotImplementedError("Condition must implement 'check' method.")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import Any, Callable, Dict
2+
3+
from .BaseCondition import BaseCondition
4+
5+
6+
class CustomCondition(BaseCondition):
7+
"""
8+
Allows users to define their own condition as a callable.
9+
"""
10+
11+
def __init__(self, condition: Callable[[Dict[str, Any]], bool]) -> None:
12+
13+
self.condition = condition
14+
15+
def check(self, data: Dict[str, Any]) -> bool:
16+
17+
return self.condition(data)

0 commit comments

Comments
 (0)