Skip to content

Instantly share code, notes, and snippets.

@fredrikbonander
Created May 27, 2013 14:01
Show Gist options
  • Save fredrikbonander/5657223 to your computer and use it in GitHub Desktop.
Save fredrikbonander/5657223 to your computer and use it in GitHub Desktop.
Ndb StringProperty validation
def _validate(self, value):
if isinstance(value, str):
# Decode from UTF-8 -- if this fails, we can't write it.
try:
value = unicode(value, 'utf-8')
except UnicodeError:
raise datastore_errors.BadValueError('Expected valid UTF-8, got %r' %
(value,))
elif not isinstance(value, unicode):
raise datastore_errors.BadValueError('Expected string, got %r' %
(value,))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment