| 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 | 109 | # Then check full URL |
| 110 | 110 | try: |
| 115 | 115 | try: |
| 116 | 116 | scheme, netloc, path, query, fragment = urlsplit(value) |
| 117 | 117 | except ValueError: # for example, "Invalid IPv6 URL" |
| 118 | raise ValidationError(self.message, code=self.code) | |
| 118 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 119 | 119 | try: |
| 120 | 120 | netloc = punycode(netloc) # IDN -> ACE |
| 121 | 121 | except UnicodeError: # invalid domain part |
| 122 | # Add value to existing error params if they exist | |
| 123 | if hasattr(e, 'params') and e.params: | |
| 124 | e.params['value'] = value | |
| 125 | else: | |
| 126 | e = ValidationError(e.message, code=e.code, params={'value': value}) | |
| 122 | 127 | raise e |
| 123 | 128 | url = urlunsplit((scheme, netloc, path, query, fragment)) |
| 124 | 129 | super().__call__(url) |
| 125 | 130 | else: |
| 126 | raise | |
| 131 | # Add value to existing error params if they exist | |
| 132 | if hasattr(e, 'params') and e.params: | |
| 133 | e.params['value'] = value | |
| 134 | else: | |
| 135 | e = ValidationError(e.message, code=e.code, params={'value': value}) | |
| 136 | raise e | |
| 127 | 137 | else: |
| 128 | 138 | # Now verify IPv6 in the netloc part |
| 129 | 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) | |
| 139 | try: | |
| 140 | host_match = re.search(r'^\[(.+)\](?::\d{2,5})?$', urlsplit(value).netloc) | |
| 141 | if host_match: | |
| 142 | potential_ip = host_match[1] | |
| 143 | try: | |
| 144 | validate_ipv6_address(potential_ip) | |
| 145 | except ValidationError: | |
| 146 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 147 | except ValueError: | |
| 148 | # urlsplit can raise ValueError for invalid IPv6 addresses | |
| 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 | 155 | if len(urlsplit(value).netloc) > 253: |
| 142 | raise ValidationError(self.message, code=self.code) | |
| 156 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 143 | 157 | |
| 144 | 158 | |
| 145 | 159 | integer_validator = RegexValidator( |
| 208 | 222 | |
| 209 | 223 | def __call__(self, value): |
| 210 | 224 | if not value or '@' not in value: |
| 211 | raise ValidationError(self.message, code=self.code) | |
| 225 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 212 | 226 | |
| 213 | 227 | user_part, domain_part = value.rsplit('@', 1) |
| 214 | 228 | |
| 215 | 229 | if not self.user_regex.match(user_part): |
| 216 | raise ValidationError(self.message, code=self.code) | |
| 230 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 217 | 231 | |
| 218 | 232 | if (domain_part not in self.domain_allowlist and |
| 219 | 233 | not self.validate_domain_part(domain_part)): |
| 225 | 239 | else: |
| 226 | 240 | if self.validate_domain_part(domain_part): |
| 227 | 241 | return |
| 228 | raise ValidationError(self.message, code=self.code) | |
| 242 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 229 | 243 | |
| 230 | 244 | def validate_domain_part(self, domain_part): |
| 231 | 245 | if self.domain_regex.match(domain_part): |
| 272 | 286 | try: |
| 273 | 287 | ipaddress.IPv4Address(value) |
| 274 | 288 | except ValueError: |
| 275 | raise ValidationError(_('Enter a valid IPv4 address.'), code='invalid') | |
| 289 | raise ValidationError(_('Enter a valid IPv4 address.'), code='invalid', params={'value': value}) | |
| 276 | 290 | |
| 277 | 291 | |
| 278 | 292 | def validate_ipv6_address(value): |
| 279 | 293 | if not is_valid_ipv6_address(value): |
| 280 | raise ValidationError(_('Enter a valid IPv6 address.'), code='invalid') | |
| 294 | raise ValidationError(_('Enter a valid IPv6 address.'), code='invalid', params={'value': value}) | |
| 281 | 295 | |
| 282 | 296 | |
| 283 | 297 | def validate_ipv46_address(value): |
| 287 | 301 | try: |
| 288 | 302 | validate_ipv6_address(value) |
| 289 | 303 | except ValidationError: |
| 290 | raise ValidationError(_('Enter a valid IPv4 or IPv6 address.'), code='invalid') | |
| 304 | raise ValidationError(_('Enter a valid IPv4 or IPv6 address.'), code='invalid', params={'value': value}) | |
| 291 | 305 | |
| 292 | 306 | |
| 293 | 307 | ip_address_validator_map = { |
| 438 | 452 | def __call__(self, value): |
| 439 | 453 | digit_tuple, exponent = value.as_tuple()[1:] |
| 440 | 454 | if exponent in {'F', 'n', 'N'}: |
| 441 | raise ValidationError(self.messages['invalid']) | |
| 455 | raise ValidationError(self.messages['invalid'], params={'value': value}) | |
| 442 | 456 | if exponent >= 0: |
| 443 | 457 | # A positive exponent adds that many trailing zeros. |
| 444 | 458 | digits = len(digit_tuple) + exponent |
| 460 | 474 | raise ValidationError( |
| 461 | 475 | self.messages['max_digits'], |
| 462 | 476 | code='max_digits', |
| 463 | params={'max': self.max_digits}, | |
| 477 | params={'max': self.max_digits, 'value': value}, | |
| 464 | 478 | ) |
| 465 | 479 | if self.decimal_places is not None and decimals > self.decimal_places: |
| 466 | 480 | raise ValidationError( |
| 467 | 481 | self.messages['max_decimal_places'], |
| 468 | 482 | code='max_decimal_places', |
| 469 | params={'max': self.decimal_places}, | |
| 483 | params={'max': self.decimal_places, 'value': value}, | |
| 470 | 484 | ) |
| 471 | 485 | if (self.max_digits is not None and self.decimal_places is not None and |
| 472 | 486 | whole_digits > (self.max_digits - self.decimal_places)): |
| 473 | 487 | raise ValidationError( |
| 474 | 488 | self.messages['max_whole_digits'], |
| 475 | 489 | code='max_whole_digits', |
| 476 | params={'max': (self.max_digits - self.decimal_places)}, | |
| 490 | params={'max': (self.max_digits - self.decimal_places), 'value': value}, | |
| 477 | 491 | ) |
| 478 | 492 | |
| 479 | 493 | def __eq__(self, other): |
| 509 | 523 | code=self.code, |
| 510 | 524 | params={ |
| 511 | 525 | 'extension': extension, |
| 512 | 'allowed_extensions': ', '.join(self.allowed_extensions) | |
| 526 | 'allowed_extensions': ', '.join(self.allowed_extensions), | |
| 527 | 'value': value | |
| 513 | 528 | } |
| 514 | 529 | ) |
| 515 | 530 | |
| 550 | 565 | |
| 551 | 566 | def __call__(self, value): |
| 552 | 567 | if '\x00' in str(value): |
| 553 | raise ValidationError(self.message, code=self.code) | |
| 568 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 554 | 569 | |
| 555 | 570 | def __eq__(self, other): |
| 556 | 571 | 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.