Skip to content

Commit

Permalink
using Unique instead of NoDuplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
bonastreyair committed Apr 19, 2021
1 parent a7054db commit b6661df
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Features:
- Add ``validate.And`` (:issue:`1768`).
Thanks :user:`rugleb` for the suggestion.
- Let ``Field``s be accessed by name as ``Schema`` attributes (:pr:`1631`).
- Add a `NoDuplicates` validator in ``marshmallow.validate`` (:pr:`1793`).
- Add a `Unique` validator in ``marshmallow.validate`` (:pr:`1793`).

Other changes:

Expand Down
4 changes: 2 additions & 2 deletions src/marshmallow/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,8 @@ def __call__(self, value: typing.Sequence[_T]) -> typing.Sequence[_T]:
return value


class NoDuplicates(Validator):
"""Validator which succeeds if the ``value`` is an ``iterable`` and has no duplicate
class Unique(Validator):
"""Validator which succeeds if the ``value`` is an ``iterable`` and has unique
elements. In case of a list of objects, it can easy check an internal
attribute by passing the ``attribute`` parameter.
Validator which fails if ``value`` is not a member of ``iterable``.
Expand Down
31 changes: 16 additions & 15 deletions tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,37 +914,38 @@ def test_and():
assert errors == ["Not an even value.", "Must be less than or equal to 6."]


def test_noduplicates():
def test_contains_unique():
class Mock:
def __init__(self, name):
self.name = name

mock_object_1 = Mock("a")
mock_object_2 = Mock("b")

assert validate.NoDuplicates()("d") == "d"
assert validate.NoDuplicates()([]) == []
assert validate.NoDuplicates()({}) == {}
assert validate.NoDuplicates()(["a", "b"]) == ["a", "b"]
assert validate.NoDuplicates()([1, 2]) == [1, 2]
assert validate.NoDuplicates(attribute="name")([mock_object_1, mock_object_2]) == [
assert validate.Unique()("d") == "d"
assert validate.Unique()([]) == []
assert validate.Unique()({}) == {}
assert validate.Unique()(["a", "b"]) == ["a", "b"]
assert validate.Unique()([1, 2]) == [1, 2]
assert validate.Unique(attribute="name")([mock_object_1, mock_object_2]) == [
mock_object_1,
mock_object_2,
]

with pytest.raises(ValidationError, match="Invalid input."):
validate.NoDuplicates()(3)
validate.Unique()(3)
with pytest.raises(ValidationError, match="Invalid input."):
validate.NoDuplicates()(1.1)
validate.Unique()(1.1)
with pytest.raises(ValidationError, match="Invalid input."):
validate.NoDuplicates()(True)
validate.Unique()(True)
with pytest.raises(ValidationError, match="Invalid input."):
validate.NoDuplicates()(None)
validate.Unique()(None)
with pytest.raises(ValidationError, match="Found a duplicate value: 1."):
validate.NoDuplicates()([1, 1, 2])
validate.Unique()([1, 1, 2])
with pytest.raises(ValidationError, match="Found a duplicate value: a."):
validate.NoDuplicates()("aab")
validate.Unique()("aab")
with pytest.raises(ValidationError, match="Found a duplicate value: a."):
validate.NoDuplicates()(["a", "a", "b"])
validate.Unique()(["a", "a", "b"])
with pytest.raises(ValidationError, match="Found a duplicate object attribute"):
validate.NoDuplicates(attribute="name")([mock_object_1, mock_object_1])
validate.Unique(attribute="name")([mock_object_1, mock_object_1])

0 comments on commit b6661df

Please sign in to comment.