Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions modularodm/validators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,15 @@ class MaxLengthValidator(BaseValidator):
clean = lambda self, x: len(x)
message = 'Ensure this value has length of at most {limit_value} (it has length {show_value}).'
code = 'max_length'


class ValueNotEmptyValidator(BaseValidator):

compare = lambda self, a, b: a == b
clean = lambda self, x: x.strip()
message = 'Ensure this value is not empty.'
code = 'not_empty'

def __init__(self):
self.limit_value = ''
super(ValueNotEmptyValidator, self).__init__(self.limit_value)
15 changes: 14 additions & 1 deletion tests/validators/test_iterable_validators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from modularodm import StoredObject
from modularodm.exceptions import ValidationValueError
from modularodm.fields import IntegerField, StringField
from modularodm.validators import MaxLengthValidator, MinLengthValidator
from modularodm.validators import MaxLengthValidator, MinLengthValidator, ValueNotEmptyValidator

from tests.base import ModularOdmTestCase

Expand All @@ -18,6 +18,10 @@ class Foo(StoredObject):
list=False,
validate=[MinLengthValidator(5), ]
)
test_field_empty = StringField(
list=False,
validate=[ValueNotEmptyValidator(), ]
)
self.test_object = Foo(_id=0)
return Foo,

Expand All @@ -39,6 +43,15 @@ def test_min_length_string_validator(self):
self.test_object.test_field_min = 'abcdefg'
self.test_object.save()

def test_value_not_empty_string_validator(self):

self.test_object.test_field_empty = ''
with self.assertRaises(ValidationValueError):
self.test_object.save()

self.test_object.test_field_empty = 'not empty'
self.test_object.save()


class ListValidatorTestCase(ModularOdmTestCase):

Expand Down