code: def validate(self, val):
          
          if not isinstance(val, six.string_types):
              raise ValidationError("'%s' expected to be a string, got %s"
                                    % (val, generic_type_name(val)))
          if not six.PY3 and isinstance(val, str):
              try:
                  val = val.decode('utf-8')
              except UnicodeDecodeError:
                  raise ValidationError("'%s' was not valid utf-8")
  
          if self.max_length is not None and len(val) > self.max_length:
              raise ValidationError("'%s' must be at most %d characters, got %d"
                                    % (val, self.max_length, len(val)))
          if self.min_length is not None and len(val) < self.min_length:
              raise ValidationError("'%s' must be at least %d characters, got %d"
                                    % (val, self.min_length, len(val)))
  
          if self.pattern and not self.pattern_re.match(val):
              raise ValidationError("'%s' did not match pattern '%s'"
                                    % (val, self.pattern))
          return val
docs: A unicode string of the correct length and pattern will pass validation.
          In PY2, we enforce that a str type must be valid utf-8, and a unicode
          string will be returned.