From d99c7c20cc049f230fa619e42119b7eada5ba4fd Mon Sep 17 00:00:00 2001 From: Pau Aliagas Date: Mon, 3 Oct 2011 16:42:10 +0200 Subject: [PATCH] Don't allow empty lists when they are required When using ListField, an empty list is added as the default value. But when you mark this field as required, you expect it not to be empty, so this patch makes sure that this is duly checked. --- mongoengine/fields.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mongoengine/fields.py b/mongoengine/fields.py index 705bf3ae..933ab4d3 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -476,6 +476,10 @@ class ListField(ComplexBaseField): isinstance(value, basestring)): raise ValidationError('Only lists and tuples may be used in a ' 'list field') + # don't allow empty lists when they are required + if self.required and not value: + raise ValidationError('Field "%s" is required and cannot be empty' % + self.name) super(ListField, self).validate(value) def prepare_query_value(self, op, value):