From ed9bc4e576d57863c10d839ee43fb2febf04fb84 Mon Sep 17 00:00:00 2001 From: Edward D'Souza Date: Tue, 23 May 2017 19:49:44 -0400 Subject: [PATCH] Made RegexValidator's inverse_match logic clearer. --- django/core/validators.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/django/core/validators.py b/django/core/validators.py index e454ca213b..390c400ce8 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -51,10 +51,12 @@ class RegexValidator: def __call__(self, value): """ - Validate that the input contains a match for the regular expression - if inverse_match is False, otherwise raise ValidationError. + Validate that the input contains (or does *not* contain, if + 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) def __eq__(self, other):