Made RegexValidator's inverse_match logic clearer.

This commit is contained in:
Edward D'Souza 2017-05-23 19:49:44 -04:00 committed by Tim Graham
parent 1b5b3710f1
commit ed9bc4e576
1 changed files with 5 additions and 3 deletions

View File

@ -51,10 +51,12 @@ class RegexValidator:
def __call__(self, value): def __call__(self, value):
""" """
Validate that the input contains a match for the regular expression Validate that the input contains (or does *not* contain, if
if inverse_match is False, otherwise raise ValidationError. inverse_match is True) a match for the regular expression.
""" """
if not (self.inverse_match is not bool(self.regex.search(str(value)))): regex_matches = bool(self.regex.search(str(value)))
invalid_input = regex_matches if self.inverse_match else not regex_matches
if invalid_input:
raise ValidationError(self.message, code=self.code) raise ValidationError(self.message, code=self.code)
def __eq__(self, other): def __eq__(self, other):