Add a max_length param to the ListField (#2107)

This is similar to the `max_length` param of a `StringField`.

Sometimes you don't want your lists to be able to grow indefinitely.
This commit is contained in:
Stefan Wójcik
2019-06-27 15:07:02 +02:00
committed by GitHub
parent b47669403b
commit 82f0eb1cbc
2 changed files with 46 additions and 1 deletions

View File

@@ -1010,6 +1010,38 @@ class FieldTest(MongoDBTestCase):
e.mapping = ["abc"]
e.save()
def test_list_field_max_length(self):
"""Ensure ListField's max_length is respected."""
class Foo(Document):
items = ListField(IntField(), max_length=5)
foo = Foo()
for i in range(1, 7):
foo.items.append(i)
if i < 6:
foo.save()
else:
with self.assertRaises(ValidationError) as cm:
foo.save()
self.assertIn("List is too long", str(cm.exception))
def test_list_field_max_length(self):
"""Ensure ListField's max_length is respected."""
class Foo(Document):
items = ListField(IntField(), max_length=5)
foo = Foo()
for i in range(1, 7):
foo.items.append(i)
if i < 6:
foo.save()
else:
with self.assertRaises(ValidationError) as cm:
foo.save()
self.assertIn("List is too long", str(cm.exception))
def test_list_field_rejects_strings(self):
"""Strings aren't valid list field data types."""