def validate(self, password, user=None): if len(password) < self.min_length: raise ValidationError( "This password is too short. It must contain at least %(min_length)d characters.", code='password_too_short', params={'min_length': self.min_length}, )
def clean_ipv6_address(ip_str, unpack_ipv4=False, error_message="This is not a valid IPv6 address."): """ Clean an IPv6 address string. Raise ValidationError if the address is invalid. Replace the longest continuous zero-sequence with "::", remove leading zeroes, and make sure all hextets are lowercase. Args: ip_str: A valid IPv6 address. unpack_ipv4: if an IPv4-mapped address is found, return the plain IPv4 address (default=False). error_message: An error message used in the ValidationError. Return a compressed IPv6 address or the same value. """ try: addr = ipaddress.IPv6Address(int(ipaddress.IPv6Address(ip_str))) except ValueError: raise ValidationError(error_message, code='invalid') if unpack_ipv4 and addr.ipv4_mapped: return str(addr.ipv4_mapped) elif addr.ipv4_mapped: return '::ffff:%s' % str(addr.ipv4_mapped) return str(addr)
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)
def __call__(self, value): cleaned = self.clean(value) params = { 'limit_value': self.limit_value, 'show_value': cleaned, 'value': value } if self.compare(cleaned, self.limit_value): raise ValidationError(self.message, code=self.code, params=params)
def validate_ipv46_address(value): try: validate_ipv4_address(value) except ValidationError: try: validate_ipv6_address(value) except ValidationError: raise ValidationError('Enter a valid IPv4 or IPv6 address.', code='invalid')
def __call__(self, value): # Check first if the scheme is valid scheme = value.split('://')[0].lower() if scheme not in self.schemes: raise ValidationError(self.message, code=self.code) # Then check full URL try: super().__call__(value) except ValidationError as e: # Trivial case failed. Try for possible IDN domain if value: try: scheme, netloc, path, query, fragment = urlsplit(value) except ValueError: # for example, "Invalid IPv6 URL" raise ValidationError(self.message, code=self.code) try: netloc = netloc.encode('idna').decode( 'ascii') # IDN -> ACE except UnicodeError: # invalid domain part raise e url = urlunsplit((scheme, netloc, path, query, fragment)) super().__call__(url) else: raise else: # Now verify IPv6 in the netloc part host_match = re.search(r'^\[(.+)\](?::\d{2,5})?$', urlsplit(value).netloc) if host_match: potential_ip = host_match.groups()[0] try: validate_ipv6_address(potential_ip) except ValidationError: raise ValidationError(self.message, code=self.code) # The maximum length of a full host name is 253 characters per RFC 1034 # section 3.1. It's defined to be 255 bytes or less, but this includes # one byte for the length of the name and one byte for the trailing dot # that's used to indicate absolute names in DNS. if len(urlsplit(value).netloc) > 253: raise ValidationError(self.message, code=self.code)
def __call__(self, value): extension = os.path.splitext(value.name)[1][1:].lower() if self.allowed_extensions is not None and extension not in self.allowed_extensions: raise ValidationError(self.message, code=self.code, params={ 'extension': extension, 'allowed_extensions': ', '.join(self.allowed_extensions) })
def __call__(self, value): if not value or '@' not in value: raise ValidationError(self.message, code=self.code) user_part, domain_part = value.rsplit('@', 1) if not self.user_regex.match(user_part): raise ValidationError(self.message, code=self.code) if (domain_part not in self.domain_whitelist and not self.validate_domain_part(domain_part)): # Try for possible IDN domain-part try: domain_part = domain_part.encode('idna').decode('ascii') except UnicodeError: ... else: if self.validate_domain_part(domain_part): return raise ValidationError(self.message, code=self.code)
def __call__(self, value): digit_tuple, exponent = value.as_tuple()[1:] if exponent >= 0: # A positive exponent adds that many trailing zeros. digits = len(digit_tuple) + exponent decimals = 0 else: # If the absolute value of the negative exponent is larger than the # number of digits, then it's the same as the number of digits, # because it'll consume all of the digits in digit_tuple and then # add abs(exponent) - len(digit_tuple) leading zeros after the # decimal point. if abs(exponent) > len(digit_tuple): digits = decimals = abs(exponent) else: digits = len(digit_tuple) decimals = abs(exponent) whole_digits = digits - decimals if self.max_digits is not None and digits > self.max_digits: raise ValidationError( self.messages['max_digits'], code='max_digits', params={'max': self.max_digits}, ) if self.decimal_places is not None and decimals > self.decimal_places: raise ValidationError( self.messages['max_decimal_places'], code='max_decimal_places', params={'max': self.decimal_places}, ) if (self.max_digits is not None and self.decimal_places is not None and whole_digits > (self.max_digits - self.decimal_places)): raise ValidationError( self.messages['max_whole_digits'], code='max_whole_digits', params={'max': (self.max_digits - self.decimal_places)}, )
def validate_password(password, user=None, password_validators=None): """ Validate whether the password meets all validator requirements. If the password is valid, return ``None``. If the password is invalid, raise ValidationError with all error messages. """ errors = [] if password_validators is None: password_validators = get_default_password_validators() for validator in password_validators: try: validator.validate(password, user) except ValidationError as error: errors.append(error) if errors: raise ValidationError(errors)
def __call__(self, value): if '\x00' in str(value): raise ValidationError(self.message, code=self.code)
def validate_ipv6_address(value): if not is_valid_ipv6_address(value): raise ValidationError('Enter a valid IPv6 address.', code='invalid')
def validate_ipv4_address(value): try: ipaddress.IPv4Address(value) except ValueError: raise ValidationError('Enter a valid IPv4 address.', code='invalid')
def validate(self, password, user=None): if password.isdigit(): raise ValidationError( "This password is entirely numeric.", code='password_entirely_numeric', )
def validate(self, password, user=None): if password.lower().strip() in self.passwords: raise ValidationError( "This password is too common.", code='password_too_common', )