| 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: |
| 111 | 111 | super().__call__(value) |
| 112 | 112 | except ValidationError as e: |
| 113 | 113 | # Trivial case failed. Try for possible IDN domain |
| 114 | if value: | |
| 114 | # Only attempt IDN processing if we can parse the URL and it might contain non-ASCII characters | |
| 115 | if value and any(ord(c) > 127 for c in value): | |
| 115 | 116 | try: |
| 116 | 117 | scheme, netloc, path, query, fragment = urlsplit(value) |
| 117 | 118 | except ValueError: # for example, "Invalid IPv6 URL" |
| 118 | raise ValidationError(self.message, code=self.code) | |
| 119 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 119 | 120 | try: |
| 120 | 121 | netloc = punycode(netloc) # IDN -> ACE |
| 121 | 122 | except UnicodeError: # invalid domain part |
| 123 | 124 | url = urlunsplit((scheme, netloc, path, query, fragment)) |
| 124 | 125 | super().__call__(url) |
| 125 | 126 | else: |
| 127 | # Re-raise the original validation error for non-IDN cases | |
| 126 | 128 | raise |
| 127 | 129 | else: |
| 128 | 130 | # Now verify IPv6 in the netloc part |
| 129 | host_match = re.search(r'^\[(.+)\](?::\d{2,5})?$', urlsplit(value).netloc) | |
| 131 | try: | |
| 132 | netloc = urlsplit(value).netloc | |
| 133 | except ValueError: # Invalid URL format | |
| 134 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 135 | host_match = re.search(r'^\[(.+)\](?::\d{2,5})?$', netloc) | |
| 130 | 136 | if host_match: |
| 131 | 137 | potential_ip = host_match[1] |
| 132 | 138 | try: |
| 133 | 139 | validate_ipv6_address(potential_ip) |
| 134 | 140 | except ValidationError: |
| 135 | raise ValidationError(self.message, code=self.code) | |
| 141 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 136 | 142 | |
| 137 | 143 | # The maximum length of a full host name is 253 characters per RFC 1034 |
| 138 | 144 | # section 3.1. It's defined to be 255 bytes or less, but this includes |
| 139 | 145 | # one byte for the length of the name and one byte for the trailing dot |
| 140 | 146 | # that's used to indicate absolute names in DNS. |
| 141 | if len(urlsplit(value).netloc) > 253: | |
| 142 | raise ValidationError(self.message, code=self.code) | |
| 147 | try: | |
| 148 | netloc = urlsplit(value).netloc | |
| 149 | except ValueError: # Invalid URL format | |
| 150 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 151 | if len(netloc) > 253: | |
| 152 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 143 | 153 | |
| 144 | 154 | |
| 145 | 155 | integer_validator = RegexValidator( |
| 208 | 218 | |
| 209 | 219 | def __call__(self, value): |
| 210 | 220 | if not value or '@' not in value: |
| 211 | raise ValidationError(self.message, code=self.code) | |
| 221 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 212 | 222 | |
| 213 | 223 | user_part, domain_part = value.rsplit('@', 1) |
| 214 | 224 | |
| 215 | 225 | if not self.user_regex.match(user_part): |
| 216 | raise ValidationError(self.message, code=self.code) | |
| 226 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 217 | 227 | |
| 218 | 228 | if (domain_part not in self.domain_allowlist and |
| 219 | 229 | not self.validate_domain_part(domain_part)): |
| 225 | 235 | else: |
| 226 | 236 | if self.validate_domain_part(domain_part): |
| 227 | 237 | return |
| 228 | raise ValidationError(self.message, code=self.code) | |
| 238 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 229 | 239 | |
| 230 | 240 | def validate_domain_part(self, domain_part): |
| 231 | 241 | if self.domain_regex.match(domain_part): |
| 272 | 282 | try: |
| 273 | 283 | ipaddress.IPv4Address(value) |
| 274 | 284 | except ValueError: |
| 275 | raise ValidationError(_('Enter a valid IPv4 address.'), code='invalid') | |
| 285 | raise ValidationError(_('Enter a valid IPv4 address.'), code='invalid', params={'value': value}) | |
| 276 | 286 | |
| 277 | 287 | |
| 278 | 288 | def validate_ipv6_address(value): |
| 279 | 289 | if not is_valid_ipv6_address(value): |
| 280 | raise ValidationError(_('Enter a valid IPv6 address.'), code='invalid') | |
| 290 | raise ValidationError(_('Enter a valid IPv6 address.'), code='invalid', params={'value': value}) | |
| 281 | 291 | |
| 282 | 292 | |
| 283 | 293 | def validate_ipv46_address(value): |
| 287 | 297 | try: |
| 288 | 298 | validate_ipv6_address(value) |
| 289 | 299 | except ValidationError: |
| 290 | raise ValidationError(_('Enter a valid IPv4 or IPv6 address.'), code='invalid') | |
| 300 | raise ValidationError(_('Enter a valid IPv4 or IPv6 address.'), code='invalid', params={'value': value}) | |
| 291 | 301 | |
| 292 | 302 | |
| 293 | 303 | ip_address_validator_map = { |
| 550 | 560 | |
| 551 | 561 | def __call__(self, value): |
| 552 | 562 | if '\x00' in str(value): |
| 553 | raise ValidationError(self.message, code=self.code) | |
| 563 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 554 | 564 | |
| 555 | 565 | def __eq__(self, other): |
| 556 | 566 | 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.