Created
October 6, 2021 09:53
-
-
Save TSKGunGun/4fc0e06ccefa359b5e5514048dc85f5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@deconstructible | |
class RegexValidator: | |
regex = '' | |
message = _('Enter a valid value.') | |
code = 'invalid' | |
inverse_match = False | |
flags = 0 | |
def __init__(self, regex=None, message=None, code=None, inverse_match=None, flags=None): | |
if regex is not None: | |
self.regex = regex | |
if message is not None: | |
self.message = message | |
if code is not None: | |
self.code = code | |
if inverse_match is not None: | |
self.inverse_match = inverse_match | |
if flags is not None: | |
self.flags = flags | |
if self.flags and not isinstance(self.regex, str): | |
raise TypeError("If the flags are set, regex must be a regular expression string.") | |
self.regex = _lazy_re_compile(self.regex, self.flags) | |
def __call__(self, value): | |
""" | |
Validate that the input contains (or does *not* contain, if | |
inverse_match is True) a match for the regular expression. | |
""" | |
regex_matches = 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, params={'value': value}) | |
def __eq__(self, other): | |
return ( | |
isinstance(other, RegexValidator) and | |
self.regex.pattern == other.regex.pattern and | |
self.regex.flags == other.regex.flags and | |
(self.message == other.message) and | |
(self.code == other.code) and | |
(self.inverse_match == other.inverse_match) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment