Skip to content

Commit 6df8c9d

Browse files
committed
Allowed to pass an instance on RegexEnum to RegexValidator directly
1 parent 529749b commit 6df8c9d

File tree

7 files changed

+29
-8
lines changed

7 files changed

+29
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class UpdateZipcodeInputFilter(InputFilter):
8888
filters=[StringTrimFilter()],
8989
validators=[
9090
RegexValidator(
91-
RegexEnum.POSTAL_CODE.value,
91+
RegexEnum.POSTAL_CODE,
9292
'The zipcode is not in the correct format.'
9393
)
9494
]

docs/source/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Changed
1111
^^^^^^^
1212
- Added deprecated warnings to old methods to ``add``, ``add_condition``, ``add_global_filter`` and
1313
``add_global_validator`` to prepare for a later removal.
14+
- Allowed to pass an instance on ``RegexEnum`` to ``RegexValidator`` directly, without needing to
15+
converting it to an string.
1416

1517

1618
[0.7.2] - 2025-09-28

docs/source/guides/frontend_validation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Example implementation
2828
filters=[StringTrimFilter()],
2929
validators=[
3030
RegexValidator(
31-
pattern=RegexEnum.POSTAL_CODE.value,
31+
pattern=RegexEnum.POSTAL_CODE,
3232
error_message='The zipcode is not in the correct format.'
3333
)
3434
]

docs/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Definition
6666
filters=[StringTrimFilter()],
6767
validators=[
6868
RegexValidator(
69-
RegexEnum.POSTAL_CODE.value,
69+
RegexEnum.POSTAL_CODE,
7070
'The zipcode is not in the correct format.'
7171
)
7272
]

flask_inputfilter/input_filter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import json
66
import logging
77
import sys
8-
from typing import TYPE_CHECKING, Any, Optional, Type, TypeVar, Union
98
import warnings
9+
from typing import TYPE_CHECKING, Any, Optional, Type, TypeVar, Union
1010

1111
from flask import Response, g, request
1212

flask_inputfilter/validators/regex_validator.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from __future__ import annotations
22

33
import re
4-
from typing import Optional
4+
from typing import Optional, Union
55

6+
from flask_inputfilter.enums import RegexEnum
67
from flask_inputfilter.exceptions import ValidationError
78
from flask_inputfilter.models import BaseValidator
89

@@ -14,7 +15,7 @@ class RegexValidator(BaseValidator):
1415
1516
**Parameters:**
1617
17-
- **pattern** (*str*): The regular expression pattern the
18+
- **pattern** (*str | RegexEnum*): The regular expression pattern the
1819
input must match.
1920
- **error_message** (*Optional[str]*): Custom error message if
2021
the input does not match the pattern.
@@ -38,10 +39,12 @@ class EmailInputFilter(InputFilter):
3839

3940
def __init__(
4041
self,
41-
pattern: str,
42+
pattern: Union[str, RegexEnum],
4243
error_message: Optional[str] = None,
4344
) -> None:
44-
self.pattern = pattern
45+
self.pattern = (
46+
pattern.value if isinstance(pattern, RegexEnum) else pattern
47+
)
4548
self.error_message = error_message
4649

4750
def validate(self, value: str) -> None:

tests/validators/test_regex_validator.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ def test_invalid_regex(self):
2222
with self.assertRaises(ValidationError):
2323
self.input_filter.validate_data({"email": "invalid_email"})
2424

25+
def test_valid_regex_using_enum(self):
26+
self.input_filter.add(
27+
"email", validators=[RegexValidator(RegexEnum.EMAIL)]
28+
)
29+
validated_data = self.input_filter.validate_data(
30+
{"email": "[email protected]"}
31+
)
32+
self.assertEqual(validated_data["email"], "[email protected]")
33+
34+
def test_invalid_regex_using_enum(self):
35+
self.input_filter.add(
36+
"email", validators=[RegexValidator(RegexEnum.EMAIL)]
37+
)
38+
with self.assertRaises(ValidationError):
39+
self.input_filter.validate_data({"email": "invalid_email"})
40+
2541
def test_custom_error_message(self):
2642
self.input_filter.add(
2743
"email",

0 commit comments

Comments
 (0)