From c23589fa3a624ecf107496d2b2e01ee769d128bf Mon Sep 17 00:00:00 2001 From: "Jasper.li" Date: Thu, 14 Dec 2017 17:27:26 +0800 Subject: [PATCH] RegexField should support unicode when use regex string --- rest_framework/fields.py | 2 ++ tests/test_fields.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index a710df7b48..107a6aeabd 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -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) diff --git a/tests/test_fields.py b/tests/test_fields.py index fc9ce192ad..49da1fde52 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -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`.