Commits

Andriy Kornatskyy  committed 27fee87

Introduced predicate rule.

  • Participants
  • Parent commits 8c2fe5d

Comments (0)

Files changed (4)

File i18n/en/LC_MESSAGES/validation.po

 "Project-Id-Version: wheezy.validation\n"
 "Report-Msgid-Bugs-To: Andriy Kornatskyy <andriy.kornatskyy@live.com>\n"
 "POT-Creation-Date: 2011-11-02 14:13+0200\n"
-"PO-Revision-Date: 2012-04-29 10:47+0200\n"
+"PO-Revision-Date: 2012-05-23 11:14+0300\n"
 "Last-Translator: Andriy Kornatskyy <andriy.kornatskyy@live.com>\n"
 "Language: US English\n"
 "MIME-Version: 1.0\n"
 "%Y/%m/%d %H:%M:%S|%m/%d/%Y %H:%M|%m/%d/%Y %H:%M:%S|%Y-%m-%d %H:%M|%Y-%m-%d "
 "%H:%M:%S|%m/%d/%y %H:%M|%m/%d/%y %H:%M:%S"
 
-#: src/wheezy/validation/model.py:131 src/wheezy/validation/model.py:132
-#: src/wheezy/validation/model.py:135
+#: src/wheezy/validation/model.py:131 src/wheezy/validation/model.py:135
 msgid "Multiple input was not in a correct format."
 msgstr "Multiple input was not in a correct format."
 
-#: src/wheezy/validation/model.py:144 src/wheezy/validation/model.py:157
-#: src/wheezy/validation/model.py:159 src/wheezy/validation/model.py:161
+#: src/wheezy/validation/model.py:144 src/wheezy/validation/model.py:159
+#: src/wheezy/validation/model.py:161
 msgid "Input was not in a correct format."
 msgstr "Input was not in a correct format."
 
 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 src/wheezy/validation/rules.py:150
 #, 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 src/wheezy/validation/rules.py:156
 #, 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 src/wheezy/validation/rules.py:163
 #, 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 src/wheezy/validation/rules.py:297
 #, 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:290
 #: src/wheezy/validation/rules.py:303
 #, 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 src/wheezy/validation/rules.py:396
+#: src/wheezy/validation/rules.py:389
+msgid "Required to satisfy validation predicate condition."
+msgstr "Required to satisfy validation predicate condition."
+
+#: src/wheezy/validation/rules.py:392 src/wheezy/validation/rules.py:420
+msgid "Required to not match validation pattern."
+msgstr "Required to not match validation pattern."
+
+#: src/wheezy/validation/rules.py:424
 msgid "Required to match validation pattern."
 msgstr "Required to match validation pattern."
 
-#: src/wheezy/validation/rules.py:326 src/wheezy/validation/rules.py:378
-#: src/wheezy/validation/rules.py:392
-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 src/wheezy/validation/rules.py:450
+#: src/wheezy/validation/rules.py:478
 msgid ""
 "Invalid slug. The value must consist of letters, digits, underscopes and/or "
 "hyphens."
 "Invalid slug. The value must consist of letters, digits, underscopes and/or "
 "hyphens."
 
-#: src/wheezy/validation/rules.py:384 src/wheezy/validation/rules.py:405
-#: src/wheezy/validation/rules.py:457 src/wheezy/validation/rules.py:481
+#: src/wheezy/validation/rules.py:509
 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 src/wheezy/validation/rules.py:528
+#: src/wheezy/validation/rules.py:556
 #, 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 src/wheezy/validation/rules.py:533
+#: src/wheezy/validation/rules.py:561
 #, 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 src/wheezy/validation/rules.py:540
+#: src/wheezy/validation/rules.py:568
 #, 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:761
-#: src/wheezy/validation/rules.py:812
+#: src/wheezy/validation/rules.py:809
+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:812 src/wheezy/validation/rules.py:840
 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 src/wheezy/validation/rules.py:781
-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:765
-#: src/wheezy/validation/rules.py:817
+#: src/wheezy/validation/rules.py:817 src/wheezy/validation/rules.py:845
 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:771
-#: src/wheezy/validation/rules.py:823
+#: src/wheezy/validation/rules.py:823 src/wheezy/validation/rules.py:851
 msgid "Exceeds maximum allowed."
 msgstr "Exceeds maximum allowed."

File i18n/ru/LC_MESSAGES/validation.po

 msgid "The value failed not equal comparison with \"%(comparand)s\"."
 msgstr "Значение равно в сравнении с \"%(comparand)s\"."
 
+#: src/wheezy/validation/rules.py:389
+msgid "Required to satisfy validation predicate condition."
+msgstr "Должно удовлетворять условие проверки предиката."
+
 #: src/wheezy/validation/rules.py:290
 msgid "Required to match validation pattern."
 msgstr "Обязательно должно соответствовать проверяемому шаблону."

File i18n/validation.po

 "Project-Id-Version: wheezy.validation\n"
 "Report-Msgid-Bugs-To: Andriy Kornatskyy <andriy.kornatskyy@live.com>\n"
 "POT-Creation-Date: 2011-11-02 14:13+0200\n"
-"PO-Revision-Date: 2012-04-29 10:47+0200\n"
+"PO-Revision-Date: 2012-05-23 11:14+0300\n"
 "Last-Translator: Andriy Kornatskyy <andriy.kornatskyy@live.com>\n"
 "Language: US English\n"
 "MIME-Version: 1.0\n"
 "%Y/%m/%d %H:%M:%S|%m/%d/%Y %H:%M|%m/%d/%Y %H:%M:%S|%Y-%m-%d %H:%M|%Y-%m-%d "
 "%H:%M:%S|%m/%d/%y %H:%M|%m/%d/%y %H:%M:%S"
 
-#: src/wheezy/validation/model.py:131 src/wheezy/validation/model.py:132
-#: src/wheezy/validation/model.py:135
+#: src/wheezy/validation/model.py:131 src/wheezy/validation/model.py:135
 msgid "Multiple input was not in a correct format."
 msgstr "Multiple input was not in a correct format."
 
-#: src/wheezy/validation/model.py:144 src/wheezy/validation/model.py:157
-#: src/wheezy/validation/model.py:159 src/wheezy/validation/model.py:161
+#: src/wheezy/validation/model.py:144 src/wheezy/validation/model.py:159
+#: src/wheezy/validation/model.py:161
 msgid "Input was not in a correct format."
 msgstr "Input was not in a correct format."
 
 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 src/wheezy/validation/rules.py:150
 #, 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 src/wheezy/validation/rules.py:156
 #, 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 src/wheezy/validation/rules.py:163
 #, 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 src/wheezy/validation/rules.py:297
 #, 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:290
 #: src/wheezy/validation/rules.py:303
 #, 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 src/wheezy/validation/rules.py:396
+#: src/wheezy/validation/rules.py:389
+msgid "Required to satisfy validation predicate condition."
+msgstr "Required to satisfy validation predicate condition."
+
+#: src/wheezy/validation/rules.py:392 src/wheezy/validation/rules.py:420
+msgid "Required to not match validation pattern."
+msgstr "Required to not match validation pattern."
+
+#: src/wheezy/validation/rules.py:424
 msgid "Required to match validation pattern."
 msgstr "Required to match validation pattern."
 
-#: src/wheezy/validation/rules.py:326 src/wheezy/validation/rules.py:378
-#: src/wheezy/validation/rules.py:392
-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 src/wheezy/validation/rules.py:450
+#: src/wheezy/validation/rules.py:478
 msgid ""
 "Invalid slug. The value must consist of letters, digits, underscopes and/or "
 "hyphens."
 "Invalid slug. The value must consist of letters, digits, underscopes and/or "
 "hyphens."
 
-#: src/wheezy/validation/rules.py:384 src/wheezy/validation/rules.py:405
-#: src/wheezy/validation/rules.py:457 src/wheezy/validation/rules.py:481
+#: src/wheezy/validation/rules.py:509
 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 src/wheezy/validation/rules.py:528
+#: src/wheezy/validation/rules.py:556
 #, 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 src/wheezy/validation/rules.py:533
+#: src/wheezy/validation/rules.py:561
 #, 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 src/wheezy/validation/rules.py:540
+#: src/wheezy/validation/rules.py:568
 #, 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:761
-#: src/wheezy/validation/rules.py:812
+#: src/wheezy/validation/rules.py:809
+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:812 src/wheezy/validation/rules.py:840
 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 src/wheezy/validation/rules.py:781
-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:765
-#: src/wheezy/validation/rules.py:817
+#: src/wheezy/validation/rules.py:817 src/wheezy/validation/rules.py:845
 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:771
-#: src/wheezy/validation/rules.py:823
+#: src/wheezy/validation/rules.py:823 src/wheezy/validation/rules.py:851
 msgid "Exceeds maximum allowed."
 msgstr "Exceeds maximum allowed."

File src/wheezy/validation/rules.py

         return True
 
 
+class PredicateRule(object):
+    """ Fails if predicate return False. Predicate is any callable
+        of the following contract::
+
+            def predicate(model):
+                return True
+
+        >>> r = PredicateRule(lambda model: model is not None)
+        >>> result = []
+        >>> r.validate('', None, 'x', result, _)
+        True
+        >>> r.validate('', None, None, result, _)
+        False
+    """
+    __slots__ = ('predicate', 'message_template')
+
+    def __init__(self, predicate, message_template=None):
+        self.predicate = predicate
+        self.message_template = message_template or _(
+                'Required to satisfy validation predicate condition.')
+
+    def validate(self, value, name, model, result, gettext):
+        if not self.predicate(model):
+            result.append(gettext(self.message_template))
+            return False
+        return True
+
+
 class RegexRule(object):
     """ Search for regular expression pattern.
     """
 missing = optional = empty = MissingRule()
 length = LengthRule
 compare = CompareRule
+predicate = PredicateRule
 regex = RegexRule
 slug = SlugRule()
 email = EmailRule()