A string field. :param kwargs: The same keyword arguments that :class:`Field` receives.
| 880 | |
| 881 | |
| 882 | class String(Field): |
| 883 | """A string field. |
| 884 | |
| 885 | :param kwargs: The same keyword arguments that :class:`Field` receives. |
| 886 | """ |
| 887 | |
| 888 | #: Default error messages. |
| 889 | default_error_messages = { |
| 890 | "invalid": "Not a valid string.", |
| 891 | "invalid_utf8": "Not a valid utf-8 string.", |
| 892 | } |
| 893 | |
| 894 | def _serialize(self, value, attr, obj, **kwargs) -> str | None: |
| 895 | if value is None: |
| 896 | return None |
| 897 | return utils.ensure_text_type(value) |
| 898 | |
| 899 | def _deserialize(self, value, attr, data, **kwargs) -> typing.Any: |
| 900 | if not isinstance(value, (str, bytes)): |
| 901 | raise self.make_error("invalid") |
| 902 | try: |
| 903 | return utils.ensure_text_type(value) |
| 904 | except UnicodeDecodeError as error: |
| 905 | raise self.make_error("invalid_utf8") from error |
| 906 | |
| 907 | |
| 908 | class UUID(String): |
no outgoing calls
no test coverage detected
searching dependent graphs…