-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prohibit null characters in CharField by default (#6073)
* Implement an allow_null_bytes argument to CharField (default True) * Switch to using native ProhibitNullCharactersValidator instead
- Loading branch information
1 parent
6618338
commit 0eb2dc1
Showing
3 changed files
with
24 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,8 @@ | |
from rest_framework import ISO_8601 | ||
from rest_framework.compat import ( | ||
MaxLengthValidator, MaxValueValidator, MinLengthValidator, | ||
MinValueValidator, unicode_repr, unicode_to_repr | ||
MinValueValidator, ProhibitNullCharactersValidator, unicode_repr, | ||
unicode_to_repr | ||
) | ||
from rest_framework.exceptions import ErrorDetail, ValidationError | ||
from rest_framework.settings import api_settings | ||
|
@@ -755,7 +756,7 @@ class CharField(Field): | |
'invalid': _('Not a valid string.'), | ||
'blank': _('This field may not be blank.'), | ||
'max_length': _('Ensure this field has no more than {max_length} characters.'), | ||
'min_length': _('Ensure this field has at least {min_length} characters.') | ||
'min_length': _('Ensure this field has at least {min_length} characters.'), | ||
} | ||
initial = '' | ||
|
||
|
@@ -778,6 +779,10 @@ def __init__(self, **kwargs): | |
self.validators.append( | ||
MinLengthValidator(self.min_length, message=message)) | ||
|
||
# ProhibitNullCharactersValidator is None on Django < 2.0 | ||
if ProhibitNullCharactersValidator is not None: | ||
self.validators.append(ProhibitNullCharactersValidator()) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
fengsi
Contributor
|
||
|
||
def run_validation(self, data=empty): | ||
# Test for the empty string here so that it does not get validated, | ||
# and so that subclasses do not need to handle it explicitly | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Unfortunately, if
validators=(x, y, z)
is passed (tuple
instead oflist
), thisappend()
would panic.I know old code does
append()
too, but still a bummer for code passingtuple
.