Andriy Kornatskyy avatar Andriy Kornatskyy committed 5b4e5dd

Added missing rule.

Comments (0)

Files changed (5)

doc/userguide.rst

 
 There are a number of validation rules defined already.
 
-* ``required``. Any value evaluated to boolean ``True`` pass this rule. See
+* ``required``. Any value evaluated to boolean ``True`` pass this rule.
+  Take also a look at ``required_but_missing`` list. See
+  :py:class:`~wheezy.validation.rules.RequiredRule`.
+* ``missing``. Any value evaluated to boolean ``False`` pass this
+  rule. Take also a look at ``required_but_missing`` list. See
   :py:class:`~wheezy.validation.rules.RequiredRule`.
 * ``length``. Result of python function ``len()`` must fall within a range
   defined by this rule. Supported range attributes include: ``min``, ``max``.

i18n/en/LC_MESSAGES/validation.po

 msgid "Required field cannot be left blank."
 msgstr "Required field cannot be left blank."
 
+#: src/wheezy/validation/rules.py:77
+msgid "Field cannot have a value."
+msgstr "Field cannot have a value."
+
 #: src/wheezy/validation/rules.py:95 src/wheezy/validation/rules.py:98
+#: src/wheezy/validation/rules.py:147
 #, python-format
 msgid "Required to be a minimum of %(min)d characters in length."
 msgstr "Required to be a minimum of %(min)d characters in length."
 
 #: src/wheezy/validation/rules.py:100 src/wheezy/validation/rules.py:103
+#: src/wheezy/validation/rules.py:152
 #, python-format
 msgid "The length must fall within the range %(min)d - %(max)d characters."
 msgstr "The length must fall within the range %(min)d - %(max)d characters."
 
 #: src/wheezy/validation/rules.py:107 src/wheezy/validation/rules.py:110
+#: src/wheezy/validation/rules.py:159
 #, python-format
 msgid "Exceeds maximum length of %(max)d."
 msgstr "Exceeds maximum length of %(max)d."
 
 #: src/wheezy/validation/rules.py:232 src/wheezy/validation/rules.py:235
+#: src/wheezy/validation/rules.py:284
 #, python-format
 msgid "The value failed equality comparison with \"%(comparand)s\"."
 msgstr "The value failed equality comparison with \"%(comparand)s\"."
 
-#: src/wheezy/validation/rules.py:238
+#: src/wheezy/validation/rules.py:238 src/wheezy/validation/rules.py:290
 #, python-format
 msgid "The value failed not equal comparison with \"%(comparand)s\"."
 msgstr "The value failed not equal comparison with \"%(comparand)s\"."
 
 #: src/wheezy/validation/rules.py:324 src/wheezy/validation/rules.py:330
+#: src/wheezy/validation/rules.py:382
 msgid "Required to match validation pattern."
 msgstr "Required to match validation pattern."
 
-#: src/wheezy/validation/rules.py:326
+#: src/wheezy/validation/rules.py:326 src/wheezy/validation/rules.py:378
 msgid "Required to not match validation pattern."
 msgstr "Required to not match validation pattern."
 
 #: src/wheezy/validation/rules.py:354 src/wheezy/validation/rules.py:375
+#: src/wheezy/validation/rules.py:427
 msgid ""
 "Invalid slug. The value must consist of letters, digits, underscopes and/or "
 "hyphens."
 "hyphens."
 
 #: src/wheezy/validation/rules.py:384 src/wheezy/validation/rules.py:405
+#: src/wheezy/validation/rules.py:457
 msgid "Required to be a valid email address."
 msgstr "Required to be a valid email address."
 
 #: src/wheezy/validation/rules.py:430 src/wheezy/validation/rules.py:451
+#: src/wheezy/validation/rules.py:503
 #, python-format
 msgid "Required to be greater or equal to %(min)s."
 msgstr "Required to be greater or equal to %(min)s."
 
 #: src/wheezy/validation/rules.py:434 src/wheezy/validation/rules.py:455
+#: src/wheezy/validation/rules.py:507
 #, python-format
 msgid "The value must fall within the range %(min)s - %(max)s"
 msgstr "The value must fall within the range %(min)s - %(max)s"
 
 #: src/wheezy/validation/rules.py:441 src/wheezy/validation/rules.py:462
+#: src/wheezy/validation/rules.py:514
 #, python-format
 msgid "Exceeds maximum allowed value of %(max)s."
 msgstr "Exceeds maximum allowed value of %(max)s."
 
 #: src/wheezy/validation/rules.py:655 src/wheezy/validation/rules.py:688
-#: src/wheezy/validation/rules.py:709
+#: src/wheezy/validation/rules.py:709 src/wheezy/validation/rules.py:761
 msgid "Required to be above a minimum allowed."
 msgstr "Required to be above a minimum allowed."
 
 #: src/wheezy/validation/rules.py:658 src/wheezy/validation/rules.py:679
+#: src/wheezy/validation/rules.py:731
 msgid "The value does not belong to the list of known items."
 msgstr "The value does not belong to the list of known items."
 
 #: src/wheezy/validation/rules.py:659 src/wheezy/validation/rules.py:692
-#: src/wheezy/validation/rules.py:713
+#: src/wheezy/validation/rules.py:713 src/wheezy/validation/rules.py:765
 msgid "Must fall within a valid range."
 msgstr "Must fall within a valid range."
 
 #: src/wheezy/validation/rules.py:665 src/wheezy/validation/rules.py:698
-#: src/wheezy/validation/rules.py:719
+#: src/wheezy/validation/rules.py:719 src/wheezy/validation/rules.py:771
 msgid "Exceeds maximum allowed."
 msgstr "Exceeds maximum allowed."

i18n/ru/LC_MESSAGES/validation.po

 msgid "Required field cannot be left blank."
 msgstr "Обязательное поле не может быть пустым."
 
+#: src/wheezy/validation/rules.py:77
+msgid "Field cannot have a value."
+msgstr "Поле не может иметь значения."
+
 #: src/wheezy/validation/rules.py:79
 #, python-format
 msgid "Required to be a minimum of %(min)d characters in length."

i18n/validation.po

 msgid "Required field cannot be left blank."
 msgstr "Required field cannot be left blank."
 
+#: src/wheezy/validation/rules.py:77
+msgid "Field cannot have a value."
+msgstr "Field cannot have a value."
+
 #: src/wheezy/validation/rules.py:95 src/wheezy/validation/rules.py:98
+#: src/wheezy/validation/rules.py:147
 #, python-format
 msgid "Required to be a minimum of %(min)d characters in length."
 msgstr "Required to be a minimum of %(min)d characters in length."
 
 #: src/wheezy/validation/rules.py:100 src/wheezy/validation/rules.py:103
+#: src/wheezy/validation/rules.py:152
 #, python-format
 msgid "The length must fall within the range %(min)d - %(max)d characters."
 msgstr "The length must fall within the range %(min)d - %(max)d characters."
 
 #: src/wheezy/validation/rules.py:107 src/wheezy/validation/rules.py:110
+#: src/wheezy/validation/rules.py:159
 #, python-format
 msgid "Exceeds maximum length of %(max)d."
 msgstr "Exceeds maximum length of %(max)d."
 
 #: src/wheezy/validation/rules.py:232 src/wheezy/validation/rules.py:235
+#: src/wheezy/validation/rules.py:284
 #, python-format
 msgid "The value failed equality comparison with \"%(comparand)s\"."
 msgstr "The value failed equality comparison with \"%(comparand)s\"."
 
-#: src/wheezy/validation/rules.py:238
+#: src/wheezy/validation/rules.py:238 src/wheezy/validation/rules.py:290
 #, python-format
 msgid "The value failed not equal comparison with \"%(comparand)s\"."
 msgstr "The value failed not equal comparison with \"%(comparand)s\"."
 
 #: src/wheezy/validation/rules.py:324 src/wheezy/validation/rules.py:330
+#: src/wheezy/validation/rules.py:382
 msgid "Required to match validation pattern."
 msgstr "Required to match validation pattern."
 
-#: src/wheezy/validation/rules.py:326
+#: src/wheezy/validation/rules.py:326 src/wheezy/validation/rules.py:378
 msgid "Required to not match validation pattern."
 msgstr "Required to not match validation pattern."
 
 #: src/wheezy/validation/rules.py:354 src/wheezy/validation/rules.py:375
+#: src/wheezy/validation/rules.py:427
 msgid ""
 "Invalid slug. The value must consist of letters, digits, underscopes and/or "
 "hyphens."
 "hyphens."
 
 #: src/wheezy/validation/rules.py:384 src/wheezy/validation/rules.py:405
+#: src/wheezy/validation/rules.py:457
 msgid "Required to be a valid email address."
 msgstr "Required to be a valid email address."
 
 #: src/wheezy/validation/rules.py:430 src/wheezy/validation/rules.py:451
+#: src/wheezy/validation/rules.py:503
 #, python-format
 msgid "Required to be greater or equal to %(min)s."
 msgstr "Required to be greater or equal to %(min)s."
 
 #: src/wheezy/validation/rules.py:434 src/wheezy/validation/rules.py:455
+#: src/wheezy/validation/rules.py:507
 #, python-format
 msgid "The value must fall within the range %(min)s - %(max)s"
 msgstr "The value must fall within the range %(min)s - %(max)s"
 
 #: src/wheezy/validation/rules.py:441 src/wheezy/validation/rules.py:462
+#: src/wheezy/validation/rules.py:514
 #, python-format
 msgid "Exceeds maximum allowed value of %(max)s."
 msgstr "Exceeds maximum allowed value of %(max)s."
 
 #: src/wheezy/validation/rules.py:655 src/wheezy/validation/rules.py:688
-#: src/wheezy/validation/rules.py:709
+#: src/wheezy/validation/rules.py:709 src/wheezy/validation/rules.py:761
 msgid "Required to be above a minimum allowed."
 msgstr "Required to be above a minimum allowed."
 
 #: src/wheezy/validation/rules.py:658 src/wheezy/validation/rules.py:679
+#: src/wheezy/validation/rules.py:731
 msgid "The value does not belong to the list of known items."
 msgstr "The value does not belong to the list of known items."
 
 #: src/wheezy/validation/rules.py:659 src/wheezy/validation/rules.py:692
-#: src/wheezy/validation/rules.py:713
+#: src/wheezy/validation/rules.py:713 src/wheezy/validation/rules.py:765
 msgid "Must fall within a valid range."
 msgstr "Must fall within a valid range."
 
 #: src/wheezy/validation/rules.py:665 src/wheezy/validation/rules.py:698
-#: src/wheezy/validation/rules.py:719
+#: src/wheezy/validation/rules.py:719 src/wheezy/validation/rules.py:771
 msgid "Exceeds maximum allowed."
 msgstr "Exceeds maximum allowed."

src/wheezy/validation/rules.py

         return True
 
 
+class MissingRule(object):
+    """ Any value evaluated to boolean ``False`` pass this rule.
+    """
+
+    def __init__(self, message_template=None):
+        self.message_template = message_template or _(
+                'Field cannot have a value.')
+
+    def __call__(self, message_template):
+        """ Let you customize message template.
+
+            >>> r = missing('customized')
+            >>> assert r != missing
+            >>> r.message_template
+            'customized'
+        """
+        return MissingRule(message_template)
+
+    def validate(self, value, name, model, result, gettext):
+        """
+            If ``value`` is evaluated to ``True`` than it cause
+            this rule to fail.
+
+            >>> result = []
+            >>> r = MissingRule(message_template='error')
+            >>> r.validate(100, None, None, result, _)
+            False
+            >>> result
+            ['error']
+
+            Anything that python interprets as ``False`` is passing
+            this rule.
+
+            >>> result = []
+            >>> r.validate('', None, None, result, _)
+            True
+            >>> result
+            []
+
+            >>> r.validate(date.min, None, None, result, _)
+            True
+
+            ``missing`` is a shortcut
+
+            >>> assert isinstance(missing, MissingRule)
+        """
+        if value and value not in required_but_missing:
+            result.append(gettext(self.message_template))
+            return False
+        return True
+
+
 class LengthRule(object):
     """ Result of python function ``len()`` must fall within a range
         defined by this rule.
 
 
 required = RequiredRule()
+missing = MissingRule()
 length = LengthRule
 compare = CompareRule
 regex = RegexRule
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.