Commits

Andriy Kornatskyy committed 28f5e33

Added equalty check for length rule.

  • Participants
  • Parent commits a1dcfb2

Comments (0)

Files changed (4)

File i18n/en/LC_MESSAGES/validation.po

 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:156
+#: src/wheezy/validation/rules.py:155
+#, python-format
+msgid "The length must be exactly %(len)d characters."
+msgstr "The length must be exactly %(len)d characters."
+
+#: src/wheezy/validation/rules.py:156 src/wheezy/validation/rules.py:161
 #, 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:163
+#: src/wheezy/validation/rules.py:163 src/wheezy/validation/rules.py:167
 #, python-format
 msgid "Exceeds maximum length of %(max)d."
 msgstr "Exceeds maximum length of %(max)d."
 
-#: src/wheezy/validation/rules.py:297
+#: src/wheezy/validation/rules.py:297 src/wheezy/validation/rules.py:330
 #, 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:303
+#: src/wheezy/validation/rules.py:303 src/wheezy/validation/rules.py:336
 #, 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:389
+#: src/wheezy/validation/rules.py:389 src/wheezy/validation/rules.py:422
 msgid "Required to satisfy validation predicate condition."
 msgstr "Required to satisfy validation predicate condition."
 
-#: src/wheezy/validation/rules.py:420
+#: src/wheezy/validation/rules.py:420 src/wheezy/validation/rules.py:453
 msgid "Required to not match validation pattern."
 msgstr "Required to not match validation pattern."
 
-#: src/wheezy/validation/rules.py:424
+#: src/wheezy/validation/rules.py:424 src/wheezy/validation/rules.py:457
 msgid "Required to match validation pattern."
 msgstr "Required to match validation pattern."
 
-#: src/wheezy/validation/rules.py:479
+#: src/wheezy/validation/rules.py:479 src/wheezy/validation/rules.py:512
 msgid ""
 "Invalid slug. The value must consist of letters, digits, underscopes and/or "
 "hyphens."
 "hyphens."
 
 #: src/wheezy/validation/rules.py:510 src/wheezy/validation/rules.py:512
+#: src/wheezy/validation/rules.py:545
 msgid "Required to be a valid email address."
 msgstr "Required to be a valid email address."
 
 #: src/wheezy/validation/rules.py:557 src/wheezy/validation/rules.py:559
+#: src/wheezy/validation/rules.py:592
 #, 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:562 src/wheezy/validation/rules.py:564
+#: src/wheezy/validation/rules.py:597
 #, 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:569 src/wheezy/validation/rules.py:571
+#: src/wheezy/validation/rules.py:604
 #, python-format
 msgid "Exceeds maximum allowed value of %(max)s."
 msgstr "Exceeds maximum allowed value of %(max)s."
 
 #: src/wheezy/validation/rules.py:810 src/wheezy/validation/rules.py:812
+#: src/wheezy/validation/rules.py:845
 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:841 src/wheezy/validation/rules.py:843
+#: src/wheezy/validation/rules.py:876
 msgid "Required to be above a minimum allowed."
 msgstr "Required to be above a minimum allowed."
 
 #: src/wheezy/validation/rules.py:846 src/wheezy/validation/rules.py:848
+#: src/wheezy/validation/rules.py:881
 msgid "Must fall within a valid range."
 msgstr "Must fall within a valid range."
 
 #: src/wheezy/validation/rules.py:852 src/wheezy/validation/rules.py:854
+#: src/wheezy/validation/rules.py:887
 msgid "Exceeds maximum allowed."
 msgstr "Exceeds maximum allowed."

File i18n/ru/LC_MESSAGES/validation.po

 msgid "Required to be a minimum of %(min)d characters in length."
 msgstr "Должно содержать, как минимум, %(min)d символов в длинну."
 
+#: src/wheezy/validation/rules.py:155
+#, python-format
+msgid "The length must be exactly %(len)d characters."
+msgstr "Должно содержать, ровно, %(len)d символов в длинну."
+
 #: src/wheezy/validation/rules.py:84
 #, python-format
 msgid "The length must fall within the range %(min)d - %(max)d characters."

File i18n/validation.po

 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:156
+#: src/wheezy/validation/rules.py:155
+#, python-format
+msgid "The length must be exactly %(len)d characters."
+msgstr "The length must be exactly %(len)d characters."
+
+#: src/wheezy/validation/rules.py:156 src/wheezy/validation/rules.py:161
 #, 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:163
+#: src/wheezy/validation/rules.py:163 src/wheezy/validation/rules.py:167
 #, python-format
 msgid "Exceeds maximum length of %(max)d."
 msgstr "Exceeds maximum length of %(max)d."
 
-#: src/wheezy/validation/rules.py:297
+#: src/wheezy/validation/rules.py:297 src/wheezy/validation/rules.py:330
 #, 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:303
+#: src/wheezy/validation/rules.py:303 src/wheezy/validation/rules.py:336
 #, 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:389
+#: src/wheezy/validation/rules.py:389 src/wheezy/validation/rules.py:422
 msgid "Required to satisfy validation predicate condition."
 msgstr "Required to satisfy validation predicate condition."
 
-#: src/wheezy/validation/rules.py:420
+#: src/wheezy/validation/rules.py:420 src/wheezy/validation/rules.py:453
 msgid "Required to not match validation pattern."
 msgstr "Required to not match validation pattern."
 
-#: src/wheezy/validation/rules.py:424
+#: src/wheezy/validation/rules.py:424 src/wheezy/validation/rules.py:457
 msgid "Required to match validation pattern."
 msgstr "Required to match validation pattern."
 
-#: src/wheezy/validation/rules.py:479
+#: src/wheezy/validation/rules.py:479 src/wheezy/validation/rules.py:512
 msgid ""
 "Invalid slug. The value must consist of letters, digits, underscopes and/or "
 "hyphens."
 "hyphens."
 
 #: src/wheezy/validation/rules.py:510 src/wheezy/validation/rules.py:512
+#: src/wheezy/validation/rules.py:545
 msgid "Required to be a valid email address."
 msgstr "Required to be a valid email address."
 
 #: src/wheezy/validation/rules.py:557 src/wheezy/validation/rules.py:559
+#: src/wheezy/validation/rules.py:592
 #, 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:562 src/wheezy/validation/rules.py:564
+#: src/wheezy/validation/rules.py:597
 #, 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:569 src/wheezy/validation/rules.py:571
+#: src/wheezy/validation/rules.py:604
 #, python-format
 msgid "Exceeds maximum allowed value of %(max)s."
 msgstr "Exceeds maximum allowed value of %(max)s."
 
 #: src/wheezy/validation/rules.py:810 src/wheezy/validation/rules.py:812
+#: src/wheezy/validation/rules.py:845
 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:841 src/wheezy/validation/rules.py:843
+#: src/wheezy/validation/rules.py:876
 msgid "Required to be above a minimum allowed."
 msgstr "Required to be above a minimum allowed."
 
 #: src/wheezy/validation/rules.py:846 src/wheezy/validation/rules.py:848
+#: src/wheezy/validation/rules.py:881
 msgid "Must fall within a valid range."
 msgstr "Must fall within a valid range."
 
 #: src/wheezy/validation/rules.py:852 src/wheezy/validation/rules.py:854
+#: src/wheezy/validation/rules.py:887
 msgid "Exceeds maximum allowed."
 msgstr "Exceeds maximum allowed."

File src/wheezy/validation/rules.py

                 self.message_template = message_template or _(
                     'Required to be a minimum of %(min)d characters'
                     ' in length.')
+            elif min == max:
+                self.validate = self.check_equal
+                self.message_template = message_template or _(
+                    'The length must be exactly %(len)d'
+                    ' characters.')
             else:
                 self.max = max
                 self.validate = self.check
                 self.message_template = message_template or _(
                     'The length must fall within the range %(min)d'
                     ' - %(max)d characters.')
+        elif max:
+            self.max = max
+            self.validate = self.check_max
+            self.message_template = message_template or _(
+                'Exceeds maximum length of %(max)d.')
         else:
-            if max:
-                self.max = max
-                self.validate = self.check_max
-                self.message_template = message_template or _(
-                    'Exceeds maximum length of %(max)d.')
-            else:
-                self.validate = self.succeed
+            self.validate = self.succeed
 
     def succeed(self, value, name, model, result, gettext):
         """
             return False
         return True
 
+    def check_equal(self, value, name, model, result, gettext):
+        """ ``check_equal`` strategy fails
+
+            >>> result = []
+            >>> r = LengthRule(min=2, max=2, message_template='len %(len)d')
+            >>> r.validate('abc', None, None, result, _)
+            False
+            >>> result
+            ['len 2']
+
+            ``check_equal`` strategy succeed
+
+            >>> result = []
+            >>> r = LengthRule(min=2, max=2)
+            >>> r.validate(None, None, None, result, _)
+            True
+            >>> r.validate('ab', None, None, result, _)
+            True
+            >>> result
+            []
+        """
+        if value is None:
+            return True
+        if len(value) != self.min:
+            result.append(gettext(self.message_template)
+                          % {'len': self.min})
+            return False
+        return True
+
     def check(self, value, name, model, result, gettext):
         """ ``check`` strategy fails