Commits

Andriy Kornatskyy committed d7bbf37

Added negated argument to RegexRule.

  • Participants
  • Parent commits 9538608

Comments (0)

Files changed (5)

File doc/userguide.rst

   ``not_equal``. See :py:class:`~wheezy.validation.rules.CompareRule`.
 * ``regex``. Search for regular expression pattern. Initialized with
   ``regex`` as a regular expression pattern or a pre-compiled regular
-  expression. See :py:class:`~wheezy.validation.rules.RegexRule`.
+  expression. Supports ``nagated`` argument.
+  See :py:class:`~wheezy.validation.rules.RegexRule`.
 * ``slug``. Ensures only letters, numbers, underscores or hyphens. See
   :py:class:`~wheezy.validation.rules.SlugRule`.
 * ``email``. Ensures a valid email. See

File i18n/en/LC_MESSAGES/validation.po

 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:324 src/wheezy/validation/rules.py:330
 msgid "Required to match validation pattern."
 msgstr "Required to match validation pattern."
 
-#: src/wheezy/validation/rules.py:354
+#: src/wheezy/validation/rules.py:326
+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
 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:384 src/wheezy/validation/rules.py:405
 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:430 src/wheezy/validation/rules.py:451
 #, 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:434 src/wheezy/validation/rules.py:455
 #, 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:441 src/wheezy/validation/rules.py:462
 #, 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
 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:658 src/wheezy/validation/rules.py:679
 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
 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
 msgid "Exceeds maximum allowed."
 msgstr "Exceeds maximum allowed."

File i18n/ru/LC_MESSAGES/validation.po

 msgid "Required to match validation pattern."
 msgstr "Обязательно должно соответствовать проверяемому шаблону."
 
+#: src/wheezy/validation/rules.py:290
+msgid "Required to not match validation pattern."
+msgstr "Обязательно не должно соответствовать проверяемому шаблону."
+
 #: src/wheezy/validation/rules.py:322
 msgid ""
 "Invalid slug. The value must consist of letters, digits, underscopes and/or "

File i18n/validation.po

 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:324 src/wheezy/validation/rules.py:330
 msgid "Required to match validation pattern."
 msgstr "Required to match validation pattern."
 
-#: src/wheezy/validation/rules.py:354
+#: src/wheezy/validation/rules.py:326
+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
 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:384 src/wheezy/validation/rules.py:405
 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:430 src/wheezy/validation/rules.py:451
 #, 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:434 src/wheezy/validation/rules.py:455
 #, 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:441 src/wheezy/validation/rules.py:462
 #, 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
 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:658 src/wheezy/validation/rules.py:679
 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
 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
 msgid "Exceeds maximum allowed."
 msgstr "Exceeds maximum allowed."

File src/wheezy/validation/rules.py

     """ Search for regular expression pattern.
     """
 
-    def __init__(self, regex=None, message_template=None):
+    def __init__(self, regex=None, negated=False, message_template=None):
         """
             ``regex`` - a regular expression pattern to search for
             or a pre-compiled regular expression.
             self.regex = re.compile(regex)
         else:
             self.regex = regex
-        self.message_template = message_template or _(
-                'Required to match validation pattern.')
+        if negated:
+            self.validate = self.check_not_found
+            self.message_template = message_template or _(
+                    'Required to not match validation pattern.')
+        else:
+            self.validate = self.check_found
+            self.message_template = message_template or _(
+                    'Required to match validation pattern.')
 
-    def validate(self, value, name, model, result, gettext):
+    def check_found(self, value, name, model, result, gettext):
         """
             >>> result = []
             >>> r = RegexRule(r'\d+')
             return False
         return True
 
+    def check_not_found(self, value, name, model, result, gettext):
+        """
+            >>> result = []
+            >>> r = RegexRule(r'\d+', negated=True)
+            >>> r.validate('1234', None, None, result, _)
+            False
+            >>> r.validate('x', None, None, result, _)
+            True
+        """
+        if self.regex.search(value):
+            result.append(gettext(self.message_template))
+            return False
+        return True
+
 
 class SlugRule(RegexRule):
     """ Ensures only letters, numbers, underscores or hyphens.
     """
 
     def __init__(self, message_template=None):
-        super(SlugRule, self).__init__(r'^[-\w]+$', message_template or _(
+        super(SlugRule, self).__init__(r'^[-\w]+$', False,
+                message_template or _(
         'Invalid slug. The value must consist of letters, '
         'digits, underscopes and/or hyphens.'))
 
 
     def __init__(self, message_template=None):
         super(EmailRule, self).__init__(
-                re.compile(r'[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}',
-                    re.IGNORECASE),
+                re.compile(r'[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,5}',
+                    re.IGNORECASE), False,
                 message_template or
                 _('Required to be a valid email address.'))