This commit is contained in:
zhenpeng 2017-12-14 10:48:12 +00:00 committed by GitHub
commit 57ba5bc703
2 changed files with 16 additions and 0 deletions

View File

@ -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)

View File

@ -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`.