Skip to content

Commit 1114b97

Browse files
committed
using Unique instead of NoDuplicates
1 parent aa82f30 commit 1114b97

3 files changed

Lines changed: 19 additions & 18 deletions

File tree

CHANGELOG.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Features:
99
- Add ``validate.And`` (:issue:`1768`).
1010
Thanks :user:`rugleb` for the suggestion.
1111
- Let ``Field``s be accessed by name as ``Schema`` attributes (:pr:`1631`).
12-
- Add a `NoDuplicates` validator in ``marshmallow.validate`` (:pr:`1793`).
12+
- Add a `Unique` validator in ``marshmallow.validate`` (:pr:`1793`).
1313

1414
Other changes:
1515

src/marshmallow/validate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,8 +646,8 @@ def __call__(self, value: typing.Sequence[_T]) -> typing.Sequence[_T]:
646646
return value
647647

648648

649-
class NoDuplicates(Validator):
650-
"""Validator which succeeds if the ``value`` is an ``iterable`` and has no duplicate
649+
class Unique(Validator):
650+
"""Validator which succeeds if the ``value`` is an ``iterable`` and has unique
651651
elements. In case of a list of objects, it can easy check an internal
652652
attribute by passing the ``attribute`` parameter.
653653
Validator which fails if ``value`` is not a member of ``iterable``.

tests/test_validate.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -914,37 +914,38 @@ def test_and():
914914
assert errors == ["Not an even value.", "Must be less than or equal to 6."]
915915

916916

917-
def test_noduplicates():
917+
def test_contains_unique():
918918
class Mock:
919919
def __init__(self, name):
920920
self.name = name
921921

922922
mock_object_1 = Mock("a")
923923
mock_object_2 = Mock("b")
924924

925-
assert validate.NoDuplicates()("d") == "d"
926-
assert validate.NoDuplicates()([]) == []
927-
assert validate.NoDuplicates()({}) == {}
928-
assert validate.NoDuplicates()(["a", "b"]) == ["a", "b"]
929-
assert validate.NoDuplicates()([1, 2]) == [1, 2]
930-
assert validate.NoDuplicates(attribute="name")([mock_object_1, mock_object_2]) == [
925+
assert validate.Unique()("d") == "d"
926+
assert validate.Unique()([]) == []
927+
assert validate.Unique()({}) == {}
928+
assert validate.Unique()(["a", "b"]) == ["a", "b"]
929+
assert validate.Unique()([1, 2]) == [1, 2]
930+
assert validate.Unique(attribute="name")([mock_object_1, mock_object_2]) == [
931931
mock_object_1,
932932
mock_object_2,
933933
]
934934

935935
with pytest.raises(ValidationError, match="Invalid input."):
936-
validate.NoDuplicates()(3)
936+
validate.Unique()(3)
937937
with pytest.raises(ValidationError, match="Invalid input."):
938-
validate.NoDuplicates()(1.1)
938+
validate.Unique()(1.1)
939939
with pytest.raises(ValidationError, match="Invalid input."):
940-
validate.NoDuplicates()(True)
940+
validate.Unique()(True)
941941
with pytest.raises(ValidationError, match="Invalid input."):
942-
validate.NoDuplicates()(None)
942+
validate.Unique()(None)
943943
with pytest.raises(ValidationError, match="Found a duplicate value: 1."):
944-
validate.NoDuplicates()([1, 1, 2])
944+
validate.Unique()([1, 1, 2])
945945
with pytest.raises(ValidationError, match="Found a duplicate value: a."):
946-
validate.NoDuplicates()("aab")
946+
validate.Unique()("aab")
947947
with pytest.raises(ValidationError, match="Found a duplicate value: a."):
948-
validate.NoDuplicates()(["a", "a", "b"])
948+
validate.Unique()(["a", "a", "b"])
949949
with pytest.raises(ValidationError, match="Found a duplicate object attribute"):
950-
validate.NoDuplicates(attribute="name")([mock_object_1, mock_object_1])
950+
validate.Unique(attribute="name")([mock_object_1, mock_object_1])
951+

0 commit comments

Comments
 (0)