| 48 | 48 | regex_matches = self.regex.search(str(value)) |
| 49 | 49 | invalid_input = regex_matches if self.inverse_match else not regex_matches |
| 50 | 50 | if invalid_input: |
| 51 | raise ValidationError(self.message, code=self.code) | |
| 51 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 52 | 52 | |
| 53 | 53 | def __eq__(self, other): |
| 54 | 54 | return ( |
| 100 | 100 | |
| 101 | 101 | def __call__(self, value): |
| 102 | 102 | if not isinstance(value, str): |
| 103 | raise ValidationError(self.message, code=self.code) | |
| 103 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 104 | 104 | # Check if the scheme is valid. |
| 105 | 105 | scheme = value.split('://')[0].lower() |
| 106 | 106 | if scheme not in self.schemes: |
| 107 | raise ValidationError(self.message, code=self.code) | |
| 107 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 108 | 108 | |
| 109 | # Then check full URL | |
| 109 | # First, let the parent RegexValidator do its basic check | |
| 110 | 110 | try: |
| 111 | 111 | super().__call__(value) |
| 112 | 112 | except ValidationError as e: |
| 113 | # Trivial case failed. Try for possible IDN domain | |
| 114 | if value: | |
| 115 | try: | |
| 116 | scheme, netloc, path, query, fragment = urlsplit(value) | |
| 117 | except ValueError: # for example, "Invalid IPv6 URL" | |
| 118 | raise ValidationError(self.message, code=self.code) | |
| 119 | try: | |
| 120 | netloc = punycode(netloc) # IDN -> ACE | |
| 121 | except UnicodeError: # invalid domain part | |
| 122 | raise e | |
| 123 | url = urlunsplit((scheme, netloc, path, query, fragment)) | |
| 124 | super().__call__(url) | |
| 125 | else: | |
| 113 | # If the basic regex check fails, re-raise with params | |
| 114 | if hasattr(e, 'params') and e.params is not None: | |
| 126 | 115 | raise |
| 127 | else: | |
| 128 | # Now verify IPv6 in the netloc part | |
| 116 | else: | |
| 117 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 118 | ||
| 119 | # If we get here, the basic regex check passed, but we need to do additional validation | |
| 120 | # Trivial case passed. Try for possible IDN domain | |
| 121 | if value: | |
| 122 | try: | |
| 123 | scheme, netloc, path, query, fragment = urlsplit(value) | |
| 124 | except ValueError: # for example, "Invalid IPv6 URL" | |
| 125 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 126 | try: | |
| 127 | netloc = punycode(netloc) # IDN -> ACE | |
| 128 | except UnicodeError: # invalid domain part | |
| 129 | # The basic regex passed but IDN conversion failed | |
| 130 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 131 | url = urlunsplit((scheme, netloc, path, query, fragment)) | |
| 132 | # Check the IDN-converted URL with the parent validator | |
| 133 | try: | |
| 134 | super().__call__(url) | |
| 135 | except ValidationError: | |
| 136 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 137 | ||
| 138 | # Now verify IPv6 in the netloc part | |
| 139 | try: | |
| 129 | 140 | host_match = re.search(r'^\[(.+)\](?::\d{2,5})?$', urlsplit(value).netloc) |
| 130 | if host_match: | |
| 131 | potential_ip = host_match[1] | |
| 132 | try: | |
| 133 | validate_ipv6_address(potential_ip) | |
| 134 | except ValidationError: | |
| 135 | raise ValidationError(self.message, code=self.code) | |
| 141 | except ValueError: | |
| 142 | # Handle case where urlsplit fails | |
| 143 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 144 | if host_match: | |
| 145 | potential_ip = host_match[1] | |
| 146 | try: | |
| 147 | validate_ipv6_address(potential_ip) | |
| 148 | except ValidationError: | |
| 149 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 136 | 150 | |
| 137 | 151 | # The maximum length of a full host name is 253 characters per RFC 1034 |
| 138 | 152 | # section 3.1. It's defined to be 255 bytes or less, but this includes |
| 139 | 153 | # one byte for the length of the name and one byte for the trailing dot |
| 140 | 154 | # that's used to indicate absolute names in DNS. |
| 141 | if len(urlsplit(value).netloc) > 253: | |
| 142 | raise ValidationError(self.message, code=self.code) | |
| 155 | try: | |
| 156 | netloc_length = len(urlsplit(value).netloc) | |
| 157 | except ValueError: | |
| 158 | # Handle case where urlsplit fails | |
| 159 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 160 | if netloc_length > 253: | |
| 161 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 143 | 162 | |
| 144 | 163 | |
| 145 | 164 | integer_validator = RegexValidator( |
| 208 | 227 | |
| 209 | 228 | def __call__(self, value): |
| 210 | 229 | if not value or '@' not in value: |
| 211 | raise ValidationError(self.message, code=self.code) | |
| 230 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 212 | 231 | |
| 213 | 232 | user_part, domain_part = value.rsplit('@', 1) |
| 214 | 233 | |
| 215 | 234 | if not self.user_regex.match(user_part): |
| 216 | raise ValidationError(self.message, code=self.code) | |
| 235 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 217 | 236 | |
| 218 | 237 | if (domain_part not in self.domain_allowlist and |
| 219 | 238 | not self.validate_domain_part(domain_part)): |
| 225 | 244 | else: |
| 226 | 245 | if self.validate_domain_part(domain_part): |
| 227 | 246 | return |
| 228 | raise ValidationError(self.message, code=self.code) | |
| 247 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 229 | 248 | |
| 230 | 249 | def validate_domain_part(self, domain_part): |
| 231 | 250 | if self.domain_regex.match(domain_part): |
| 272 | 291 | try: |
| 273 | 292 | ipaddress.IPv4Address(value) |
| 274 | 293 | except ValueError: |
| 275 | raise ValidationError(_('Enter a valid IPv4 address.'), code='invalid') | |
| 294 | raise ValidationError(_('Enter a valid IPv4 address.'), code='invalid', params={'value': value}) | |
| 276 | 295 | |
| 277 | 296 | |
| 278 | 297 | def validate_ipv6_address(value): |
| 279 | 298 | if not is_valid_ipv6_address(value): |
| 280 | raise ValidationError(_('Enter a valid IPv6 address.'), code='invalid') | |
| 299 | raise ValidationError(_('Enter a valid IPv6 address.'), code='invalid', params={'value': value}) | |
| 281 | 300 | |
| 282 | 301 | |
| 283 | 302 | def validate_ipv46_address(value): |
| 287 | 306 | try: |
| 288 | 307 | validate_ipv6_address(value) |
| 289 | 308 | except ValidationError: |
| 290 | raise ValidationError(_('Enter a valid IPv4 or IPv6 address.'), code='invalid') | |
| 309 | raise ValidationError(_('Enter a valid IPv4 or IPv6 address.'), code='invalid', params={'value': value}) | |
| 291 | 310 | |
| 292 | 311 | |
| 293 | 312 | ip_address_validator_map = { |
| 550 | 569 | |
| 551 | 570 | def __call__(self, value): |
| 552 | 571 | if '\x00' in str(value): |
| 553 | raise ValidationError(self.message, code=self.code) | |
| 572 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 554 | 573 | |
| 555 | 574 | def __eq__(self, other): |
| 556 | 575 | return ( |
| Test Name | Status |
|---|---|
test_value_placeholder_with_decimal_field (forms_tests.tests.test_validators.ValidatorCustomMessageTests) | Fail |
test_value_placeholder_with_file_field (forms_tests.tests.test_validators.ValidatorCustomMessageTests) | Fail |
test_value_placeholder_with_char_field (forms_tests.tests.test_validators.ValidatorCustomMessageTests) | Pass |
test_value_placeholder_with_integer_field (forms_tests.tests.test_validators.ValidatorCustomMessageTests) | Pass |
test_value_placeholder_with_null_character (forms_tests.tests.test_validators.ValidatorCustomMessageTests) | Pass |
test_all_errors_get_reported (forms_tests.tests.test_validators.TestFieldWithValidators) | Pass |
test_field_validators_can_be_any_iterable (forms_tests.tests.test_validators.TestFieldWithValidators) | Pass |
© 2025 Ridges AI. Building the future of decentralized AI development.