| 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 ( |
| 126 | 126 | raise |
| 127 | 127 | else: |
| 128 | 128 | # 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) | |
| 129 | try: | |
| 130 | host_match = re.search(r'^\[(.+)\](?::\d{2,5})?$', urlsplit(value).netloc) | |
| 131 | if host_match: | |
| 132 | potential_ip = host_match[1] | |
| 133 | try: | |
| 134 | validate_ipv6_address(potential_ip) | |
| 135 | except ValidationError: | |
| 136 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 137 | except ValueError: | |
| 138 | # urlsplit can raise ValueError for malformed URLs | |
| 139 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 136 | 140 | |
| 137 | 141 | # The maximum length of a full host name is 253 characters per RFC 1034 |
| 138 | 142 | # section 3.1. It's defined to be 255 bytes or less, but this includes |
| 139 | 143 | # one byte for the length of the name and one byte for the trailing dot |
| 140 | 144 | # that's used to indicate absolute names in DNS. |
| 141 | if len(urlsplit(value).netloc) > 253: | |
| 142 | raise ValidationError(self.message, code=self.code) | |
| 145 | try: | |
| 146 | if len(urlsplit(value).netloc) > 253: | |
| 147 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 148 | except ValueError: | |
| 149 | # urlsplit can raise ValueError for malformed URLs | |
| 150 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 143 | 151 | |
| 144 | 152 | |
| 145 | 153 | integer_validator = RegexValidator( |
| 208 | 216 | |
| 209 | 217 | def __call__(self, value): |
| 210 | 218 | if not value or '@' not in value: |
| 211 | raise ValidationError(self.message, code=self.code) | |
| 219 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 212 | 220 | |
| 213 | 221 | user_part, domain_part = value.rsplit('@', 1) |
| 214 | 222 | |
| 215 | 223 | if not self.user_regex.match(user_part): |
| 216 | raise ValidationError(self.message, code=self.code) | |
| 224 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 217 | 225 | |
| 218 | 226 | if (domain_part not in self.domain_allowlist and |
| 219 | 227 | not self.validate_domain_part(domain_part)): |
| 225 | 233 | else: |
| 226 | 234 | if self.validate_domain_part(domain_part): |
| 227 | 235 | return |
| 228 | raise ValidationError(self.message, code=self.code) | |
| 236 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 229 | 237 | |
| 230 | 238 | def validate_domain_part(self, domain_part): |
| 231 | 239 | if self.domain_regex.match(domain_part): |
| 272 | 280 | try: |
| 273 | 281 | ipaddress.IPv4Address(value) |
| 274 | 282 | except ValueError: |
| 275 | raise ValidationError(_('Enter a valid IPv4 address.'), code='invalid') | |
| 283 | raise ValidationError(_('Enter a valid IPv4 address.'), code='invalid', params={'value': value}) | |
| 276 | 284 | |
| 277 | 285 | |
| 278 | 286 | def validate_ipv6_address(value): |
| 279 | 287 | if not is_valid_ipv6_address(value): |
| 280 | raise ValidationError(_('Enter a valid IPv6 address.'), code='invalid') | |
| 288 | raise ValidationError(_('Enter a valid IPv6 address.'), code='invalid', params={'value': value}) | |
| 281 | 289 | |
| 282 | 290 | |
| 283 | 291 | def validate_ipv46_address(value): |
| 287 | 295 | try: |
| 288 | 296 | validate_ipv6_address(value) |
| 289 | 297 | except ValidationError: |
| 290 | raise ValidationError(_('Enter a valid IPv4 or IPv6 address.'), code='invalid') | |
| 298 | raise ValidationError(_('Enter a valid IPv4 or IPv6 address.'), code='invalid', params={'value': value}) | |
| 291 | 299 | |
| 292 | 300 | |
| 293 | 301 | ip_address_validator_map = { |
| 509 | 517 | code=self.code, |
| 510 | 518 | params={ |
| 511 | 519 | 'extension': extension, |
| 512 | 'allowed_extensions': ', '.join(self.allowed_extensions) | |
| 520 | 'allowed_extensions': ', '.join(self.allowed_extensions), | |
| 521 | 'value': value | |
| 513 | 522 | } |
| 514 | 523 | ) |
| 515 | 524 | |
| 550 | 559 | |
| 551 | 560 | def __call__(self, value): |
| 552 | 561 | if '\x00' in str(value): |
| 553 | raise ValidationError(self.message, code=self.code) | |
| 562 | raise ValidationError(self.message, code=self.code, params={'value': value}) | |
| 554 | 563 | |
| 555 | 564 | def __eq__(self, other): |
| 556 | 565 | return ( |
| Test Name | Status |
|---|---|
test_value_placeholder_with_char_field (forms_tests.tests.test_validators.ValidatorCustomMessageTests) | Fail |
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_integer_field (forms_tests.tests.test_validators.ValidatorCustomMessageTests) | Fail |
test_value_placeholder_with_null_character (forms_tests.tests.test_validators.ValidatorCustomMessageTests) | Fail |
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.