Skip to main content

RegexValidator

This class provides a mechanism to validate that a given input matches, or optionally does not match, a specified regular expression. It supports custom error messages, error codes, and regex flags to control matching behavior. The validator is designed to be used as a callable that raises a validation error when the input fails the defined criteria.

Attributes

AttributeTypeDescription
regexstringThe regular expression pattern or compiled regex object used to validate the input value.
messagestring = Enter a valid value.The error message used in the ValidationError if the validation fails.
codestring = invalidThe error code used in the ValidationError if the validation fails.
inverse_matchboolean = falseDetermines whether to invert the validation logic, raising an error if a match is found instead of when it is missing.
flagsinteger = 0The regular expression flags used when compiling the regex string, such as re.IGNORECASE.

Constructor

Signature

def RegexValidator(
regex: string|Pattern = None,
message: string = None,
code: string = None,
inverse_match: boolean = None,
flags: int = None
)

Parameters

NameTypeDescription
regex`stringPattern` = None
messagestring = NoneThe error message to raise on validation failure.
codestring = NoneThe error code to associate with the validation failure.
inverse_matchboolean = NoneIf True, validation fails if the regex matches; if False, validation fails if it does not match.
flagsint = NoneRegular expression compilation flags.