| 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 | params = {'value': value} if '%(value)s' in str(self.message) else None | |
| 52 | raise ValidationError(self.message, code=self.code, params=params) | |
| 52 | 53 | |
| 53 | 54 | def __eq__(self, other): |
| 54 | 55 | return ( |
| 100 | 101 | |
| 101 | 102 | def __call__(self, value): |
| 102 | 103 | if not isinstance(value, str): |
| 103 | raise ValidationError(self.message, code=self.code) | |
| 104 | params = {'value': value} if '%(value)s' in str(self.message) else None | |
| 105 | raise ValidationError(self.message, code=self.code, params=params) | |
| 104 | 106 | # Check if the scheme is valid. |
| 105 | 107 | scheme = value.split('://')[0].lower() |
| 106 | 108 | if scheme not in self.schemes: |
| 107 | raise ValidationError(self.message, code=self.code) | |
| 109 | params = {'value': value} if '%(value)s' in str(self.message) else None | |
| 110 | raise ValidationError(self.message, code=self.code, params=params) | |
| 108 | 111 | |
| 109 | 112 | # Then check full URL |
| 110 | 113 | try: |
| 115 | 118 | try: |
| 116 | 119 | scheme, netloc, path, query, fragment = urlsplit(value) |
| 117 | 120 | except ValueError: # for example, "Invalid IPv6 URL" |
| 118 | raise ValidationError(self.message, code=self.code) | |
| 121 | params = {'value': value} if '%(value)s' in str(self.message) else None | |
| 122 | raise ValidationError(self.message, code=self.code, params=params) | |
| 119 | 123 | try: |
| 120 | 124 | netloc = punycode(netloc) # IDN -> ACE |
| 121 | 125 | except UnicodeError: # invalid domain part |
| 132 | 136 | try: |
| 133 | 137 | validate_ipv6_address(potential_ip) |
| 134 | 138 | except ValidationError: |
| 135 | raise ValidationError(self.message, code=self.code) | |
| 139 | params = {'value': value} if '%(value)s' in str(self.message) else None | |
| 140 | raise ValidationError(self.message, code=self.code, params=params) | |
| 136 | 141 | |
| 137 | 142 | # The maximum length of a full host name is 253 characters per RFC 1034 |
| 138 | 143 | # section 3.1. It's defined to be 255 bytes or less, but this includes |
| 139 | 144 | # one byte for the length of the name and one byte for the trailing dot |
| 140 | 145 | # that's used to indicate absolute names in DNS. |
| 141 | 146 | if len(urlsplit(value).netloc) > 253: |
| 142 | raise ValidationError(self.message, code=self.code) | |
| 147 | params = {'value': value} if '%(value)s' in str(self.message) else None | |
| 148 | raise ValidationError(self.message, code=self.code, params=params) | |
| 143 | 149 | |
| 144 | 150 | |
| 145 | 151 | integer_validator = RegexValidator( |
| 208 | 214 | |
| 209 | 215 | def __call__(self, value): |
| 210 | 216 | if not value or '@' not in value: |
| 211 | raise ValidationError(self.message, code=self.code) | |
| 217 | params = {'value': value} if '%(value)s' in str(self.message) else None | |
| 218 | raise ValidationError(self.message, code=self.code, params=params) | |
| 212 | 219 | |
| 213 | 220 | user_part, domain_part = value.rsplit('@', 1) |
| 214 | 221 | |
| 215 | 222 | if not self.user_regex.match(user_part): |
| 216 | raise ValidationError(self.message, code=self.code) | |
| 223 | params = {'value': value} if '%(value)s' in str(self.message) else None | |
| 224 | raise ValidationError(self.message, code=self.code, params=params) | |
| 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 | params = {'value': value} if '%(value)s' in str(self.message) else None | |
| 237 | raise ValidationError(self.message, code=self.code, params=params) | |
| 229 | 238 | |
| 230 | 239 | def validate_domain_part(self, domain_part): |
| 231 | 240 | if self.domain_regex.match(domain_part): |
| 272 | 281 | try: |
| 273 | 282 | ipaddress.IPv4Address(value) |
| 274 | 283 | except ValueError: |
| 275 | raise ValidationError(_('Enter a valid IPv4 address.'), code='invalid') | |
| 284 | message = _('Enter a valid IPv4 address.') | |
| 285 | params = {'value': value} if '%(value)s' in str(message) else None | |
| 286 | raise ValidationError(message, code='invalid', params=params) | |
| 276 | 287 | |
| 277 | 288 | |
| 278 | 289 | def validate_ipv6_address(value): |
| 279 | 290 | if not is_valid_ipv6_address(value): |
| 280 | raise ValidationError(_('Enter a valid IPv6 address.'), code='invalid') | |
| 291 | message = _('Enter a valid IPv6 address.') | |
| 292 | params = {'value': value} if '%(value)s' in str(message) else None | |
| 293 | raise ValidationError(message, code='invalid', params=params) | |
| 281 | 294 | |
| 282 | 295 | |
| 283 | 296 | def validate_ipv46_address(value): |
| 287 | 300 | try: |
| 288 | 301 | validate_ipv6_address(value) |
| 289 | 302 | except ValidationError: |
| 290 | raise ValidationError(_('Enter a valid IPv4 or IPv6 address.'), code='invalid') | |
| 303 | message = _('Enter a valid IPv4 or IPv6 address.') | |
| 304 | params = {'value': value} if '%(value)s' in str(message) else None | |
| 305 | raise ValidationError(message, code='invalid', params=params) | |
| 291 | 306 | |
| 292 | 307 | |
| 293 | 308 | ip_address_validator_map = { |
| 438 | 453 | def __call__(self, value): |
| 439 | 454 | digit_tuple, exponent = value.as_tuple()[1:] |
| 440 | 455 | if exponent in {'F', 'n', 'N'}: |
| 441 | raise ValidationError(self.messages['invalid']) | |
| 456 | message = self.messages['invalid'] | |
| 457 | params = {'value': value} if '%(value)s' in str(message) else None | |
| 458 | raise ValidationError(message, params=params) | |
| 442 | 459 | if exponent >= 0: |
| 443 | 460 | # A positive exponent adds that many trailing zeros. |
| 444 | 461 | digits = len(digit_tuple) + exponent |
| 457 | 474 | whole_digits = digits - decimals |
| 458 | 475 | |
| 459 | 476 | if self.max_digits is not None and digits > self.max_digits: |
| 477 | message = self.messages['max_digits'] | |
| 478 | params = {'max': self.max_digits} | |
| 479 | if '%(value)s' in str(message): | |
| 480 | params['value'] = value | |
| 460 | 481 | raise ValidationError( |
| 461 | self.messages['max_digits'], | |
| 482 | message, | |
| 462 | 483 | code='max_digits', |
| 463 | params={'max': self.max_digits}, | |
| 484 | params=params, | |
| 464 | 485 | ) |
| 465 | 486 | if self.decimal_places is not None and decimals > self.decimal_places: |
| 487 | message = self.messages['max_decimal_places'] | |
| 488 | params = {'max': self.decimal_places} | |
| 489 | if '%(value)s' in str(message): | |
| 490 | params['value'] = value | |
| 466 | 491 | raise ValidationError( |
| 467 | self.messages['max_decimal_places'], | |
| 492 | message, | |
| 468 | 493 | code='max_decimal_places', |
| 469 | params={'max': self.decimal_places}, | |
| 494 | params=params, | |
| 470 | 495 | ) |
| 471 | 496 | if (self.max_digits is not None and self.decimal_places is not None and |
| 472 | 497 | whole_digits > (self.max_digits - self.decimal_places)): |
| 498 | message = self.messages['max_whole_digits'] | |
| 499 | params = {'max': (self.max_digits - self.decimal_places)} | |
| 500 | if '%(value)s' in str(message): | |
| 501 | params['value'] = value | |
| 473 | 502 | raise ValidationError( |
| 474 | self.messages['max_whole_digits'], | |
| 503 | message, | |
| 475 | 504 | code='max_whole_digits', |
| 476 | params={'max': (self.max_digits - self.decimal_places)}, | |
| 505 | params=params, | |
| 477 | 506 | ) |
| 478 | 507 | |
| 479 | 508 | def __eq__(self, other): |
| 504 | 533 | def __call__(self, value): |
| 505 | 534 | extension = Path(value.name).suffix[1:].lower() |
| 506 | 535 | if self.allowed_extensions is not None and extension not in self.allowed_extensions: |
| 536 | params = { | |
| 537 | 'extension': extension, | |
| 538 | 'allowed_extensions': ', '.join(self.allowed_extensions) | |
| 539 | } | |
| 540 | if '%(value)s' in str(self.message): | |
| 541 | params['value'] = value | |
| 507 | 542 | raise ValidationError( |
| 508 | 543 | self.message, |
| 509 | 544 | code=self.code, |
| 510 | params={ | |
| 511 | 'extension': extension, | |
| 512 | 'allowed_extensions': ', '.join(self.allowed_extensions) | |
| 513 | } | |
| 545 | params=params | |
| 514 | 546 | ) |
| 515 | 547 | |
| 516 | 548 | def __eq__(self, other): |
| 550 | 582 | |
| 551 | 583 | def __call__(self, value): |
| 552 | 584 | if '\x00' in str(value): |
| 553 | raise ValidationError(self.message, code=self.code) | |
| 585 | params = {'value': value} if '%(value)s' in str(self.message) else None | |
| 586 | raise ValidationError(self.message, code=self.code, params=params) | |
| 554 | 587 | |
| 555 | 588 | def __eq__(self, other): |
| 556 | 589 | 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) | Fail |
test_field_validators_can_be_any_iterable (forms_tests.tests.test_validators.TestFieldWithValidators) | Fail |
© 2025 Ridges AI. Building the future of decentralized AI development.