Skip to content

Commit

Permalink
fix: NoneOf and AnyOf can validate multiple valued fields like `S…
Browse files Browse the repository at this point in the history
…electMultipleField`
  • Loading branch information
azmeuk committed Oct 10, 2023
1 parent 28514da commit 2333642
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Version 3.x.x

Unreleased

- :class:`~wtforms.validators.NoneOf` and :class:`~wtforms.validators.AnyOf`
can validate multiple valued fields like :class:`~fields.SelectMultipleField`
:pr:`538` :pr:`807`
- Display :class:`~wtforms.Flags` values in their repr

Version 3.1.0
Expand Down
6 changes: 4 additions & 2 deletions src/wtforms/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ def __init__(self, values, message=None, values_formatter=None):
self.values_formatter = values_formatter

def __call__(self, form, field):
if field.data in self.values:
data = field.data if isinstance(field.data, list) else [field.data]
if any(d in self.values for d in data):
return

message = self.message
Expand Down Expand Up @@ -621,7 +622,8 @@ def __init__(self, values, message=None, values_formatter=None):
self.values_formatter = values_formatter

def __call__(self, form, field):
if field.data not in self.values:
data = field.data if isinstance(field.data, list) else [field.data]
if not any(d in self.values for d in data):
return

message = self.message
Expand Down
15 changes: 15 additions & 0 deletions tests/validators/test_anyof.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,18 @@ def formatter(values):
validator(dummy_form, dummy_field)

assert str(e.value) == "test 9::8::7"


def test_none_multiple_values(dummy_form, dummy_field):
"""
the validator should work with multiple values like produced
by SelectMultiple fields
"""
dummy_field.data = ["a", "e"]
validator = AnyOf(["a", "b", "c"])
validator(dummy_form, dummy_field)

dummy_field.data = ["d", "e"]
validator = AnyOf(["a", "b", "c"])
with pytest.raises(ValueError):
validator(dummy_form, dummy_field)
15 changes: 15 additions & 0 deletions tests/validators/test_noneof.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,18 @@ def test_none_of_raises(dummy_form, dummy_field):
validator = NoneOf(["a", "b", "c"])
with pytest.raises(ValueError):
validator(dummy_form, dummy_field)


def test_none_of_multiple_values(dummy_form, dummy_field):
"""
the validator should work with multiple values like produced
by SelectMultiple fields
"""
dummy_field.data = ["d", "e"]
validator = NoneOf(["a", "b", "c"])
validator(dummy_form, dummy_field)

dummy_field.data = ["a", "e"]
validator = NoneOf(["a", "b", "c"])
with pytest.raises(ValueError):
validator(dummy_form, dummy_field)

0 comments on commit 2333642

Please sign in to comment.