-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Enable Validators to defer string evaluation and handle new string format #3438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
1f20cb7
87aff64
6007bc7
1ce88be
3a4be04
026e114
e392ee7
fd34388
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -675,11 +675,11 @@ def __init__(self, **kwargs): | |
| self.min_length = kwargs.pop('min_length', None) | ||
| super(CharField, self).__init__(**kwargs) | ||
| if self.max_length is not None: | ||
| message = self.error_messages['max_length'].format(max_length=self.max_length) | ||
| self.validators.append(MaxLengthValidator(self.max_length, message=message)) | ||
| message = self.error_messages['max_length'] | ||
| self.validators.append(MaxLengthValidator(self.max_length, message=message, string_format='{')) | ||
| if self.min_length is not None: | ||
| message = self.error_messages['min_length'].format(min_length=self.min_length) | ||
| self.validators.append(MinLengthValidator(self.min_length, message=message)) | ||
| message = self.error_messages['min_length'] | ||
| self.validators.append(MinLengthValidator(self.min_length, message=message, string_format='{')) | ||
|
|
||
| def run_validation(self, data=empty): | ||
| # Test for the empty string here so that it does not get validated, | ||
|
|
@@ -820,11 +820,11 @@ def __init__(self, **kwargs): | |
| self.min_value = kwargs.pop('min_value', None) | ||
| super(IntegerField, self).__init__(**kwargs) | ||
| if self.max_value is not None: | ||
| message = self.error_messages['max_value'].format(max_value=self.max_value) | ||
| self.validators.append(MaxValueValidator(self.max_value, message=message)) | ||
| message = self.error_messages['max_value'] | ||
| self.validators.append(MaxValueValidator(self.max_value, message=message, string_format='{')) | ||
| if self.min_value is not None: | ||
| message = self.error_messages['min_value'].format(min_value=self.min_value) | ||
| self.validators.append(MinValueValidator(self.min_value, message=message)) | ||
| message = self.error_messages['min_value'] | ||
| self.validators.append(MinValueValidator(self.min_value, message=message, string_format='{')) | ||
|
|
||
| def to_internal_value(self, data): | ||
| if isinstance(data, six.text_type) and len(data) > self.MAX_STRING_LENGTH: | ||
|
|
@@ -854,11 +854,11 @@ def __init__(self, **kwargs): | |
| self.min_value = kwargs.pop('min_value', None) | ||
| super(FloatField, self).__init__(**kwargs) | ||
| if self.max_value is not None: | ||
| message = self.error_messages['max_value'].format(max_value=self.max_value) | ||
| self.validators.append(MaxValueValidator(self.max_value, message=message)) | ||
| message = self.error_messages['max_value'] | ||
| self.validators.append(MaxValueValidator(self.max_value, message=message, string_format='{')) | ||
| if self.min_value is not None: | ||
| message = self.error_messages['min_value'].format(min_value=self.min_value) | ||
| self.validators.append(MinValueValidator(self.min_value, message=message)) | ||
| message = self.error_messages['min_value'] | ||
| self.validators.append(MinValueValidator(self.min_value, message=message, string_format='{')) | ||
|
|
||
| def to_internal_value(self, data): | ||
| if isinstance(data, six.text_type) and len(data) > self.MAX_STRING_LENGTH: | ||
|
|
@@ -903,11 +903,11 @@ def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value= | |
| super(DecimalField, self).__init__(**kwargs) | ||
|
|
||
| if self.max_value is not None: | ||
| message = self.error_messages['max_value'].format(max_value=self.max_value) | ||
| self.validators.append(MaxValueValidator(self.max_value, message=message)) | ||
| message = self.error_messages['max_value'] | ||
| self.validators.append(MaxValueValidator(self.max_value, message=message, string_format='{')) | ||
| if self.min_value is not None: | ||
| message = self.error_messages['min_value'].format(min_value=self.min_value) | ||
| self.validators.append(MinValueValidator(self.min_value, message=message)) | ||
| message = self.error_messages['min_value'] | ||
| self.validators.append(MinValueValidator(self.min_value, message=message, string_format='{')) | ||
|
|
||
| def to_internal_value(self, data): | ||
| """ | ||
|
|
@@ -1606,8 +1606,8 @@ def __init__(self, model_field, **kwargs): | |
| max_length = kwargs.pop('max_length', None) | ||
| super(ModelField, self).__init__(**kwargs) | ||
| if max_length is not None: | ||
| message = self.error_messages['max_length'].format(max_length=max_length) | ||
| self.validators.append(MaxLengthValidator(max_length, message=message)) | ||
| message = self.error_messages['max_length'] | ||
| self.validators.append(MaxLengthValidator(max_length, message=message, string_format='{')) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think for this to be acceptable we'd need to find a way that doesn't require passing
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tomchristie move all the messages not to use the new format style but it will impact users that did customize it.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we not eg. silently try both?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to think about this a bit more. Note that it likely won't fail in most cases: |
||
|
|
||
| def to_internal_value(self, data): | ||
| rel = getattr(self.model_field, 'rel', None) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather see us keep the existing style.
At any rate this (looks like?) an unrelated change, so if you feel strongly about it and do want it in, perhaps let's discuss that in the context of a separate PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If by existing style you mean the if else then it's way more than just style.
The Django 1.8+ validators force the string evaluation during init which in turn breaks the deferred evaluation we are trying to setup.
I discussed that with some core dev which agreed it could be an improvement on Django. Meanwhile, it the second part of the bug will not work with django 1.8.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically, Django does a condition on the provided message. The issue is that the test forces the string evaluation which voids the deferred part.
By using pop instead of testing against the parameter we workaround this