Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

Commit a50e0e3

Browse files
exact_items validation for Array
1 parent f772992 commit a50e0e3

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

coreapi/typesys.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ def validate(self, value, definitions=None):
350350

351351
validated = []
352352

353+
if self.min_items is not None and self.min_items == self.max_items and len(value) != self.min_items:
354+
self.error('exact_items')
353355
if self.min_items is not None and len(value) < self.min_items:
354356
if self.min_items == 1:
355357
self.error('empty')

tests/typesys/test_array.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ def test_array_empty():
5353
assert exc.value.detail == 'Must not be empty.'
5454

5555

56+
def test_array_exact_count():
57+
schema = Array(min_items=3, max_items=3)
58+
assert schema.validate([1, 2, 3]) == [1, 2, 3]
59+
with pytest.raises(ValidationError) as exc:
60+
schema.validate([1, 2, 3, 4])
61+
assert exc.value.detail == 'Must have 3 items.'
62+
63+
5664
def test_array_unique_items():
5765
schema = Array(unique_items=True)
5866
assert schema.validate([1, 2, 3]) == [1, 2, 3]

0 commit comments

Comments
 (0)