Skip to content
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

RegexField should support unicode when use regex string #5669

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,8 @@ class RegexField(CharField):

def __init__(self, regex, **kwargs):
super(RegexField, self).__init__(**kwargs)
if isinstance(regex, six.string_types):
regex = re.compile(regex, re.UNICODE)
validator = RegexValidator(regex, message=self.error_messages['invalid'])
self.validators.append(validator)

Expand Down
14 changes: 14 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,20 @@ class TestiCompiledRegexField(FieldValues):
field = serializers.RegexField(regex=re.compile('[a-z][0-9]'))


class TestUnicodeRegexField(FieldValues):
"""
Valid and invalid values for `RegexField`.
"""
valid_inputs = {
u'hello\u4f60\u597d': u'hello\u4f60\u597d'
}
invalid_inputs = {
u'hello\u4f60\u597d@': ["This value does not match the required pattern."]
}
outputs = {}
field = serializers.RegexField(regex='^\w*$')


class TestSlugField(FieldValues):
"""
Valid and invalid values for `SlugField`.
Expand Down