Skip to content

Instantly share code, notes, and snippets.

@JensRantil
Created June 24, 2013 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JensRantil/5854146 to your computer and use it in GitHub Desktop.
Save JensRantil/5854146 to your computer and use it in GitHub Desktop.
Custom schematics type.
class EventId(types.BaseType):
"""Schema type for aggregate event id."""
DIVIDER = ':'
def to_primitive(self, value):
if not isinstance(value, tuple):
raise ValidationError('{0} is not a tuple'.format(value))
if len(value) != 2:
raise ValidationError('{0} does not have two items'.format(value))
prim = EventId.DIVIDER.join(str(v) for v in value)
return prim
def convert(self, value):
if not isinstance(value, basestring):
raise ValidationError('{0} is not a string'.format(value))
pieces = value.split(EventId.DIVIDER)
if len(pieces) != 2:
raise ValidationError(("{0} had more than one"
" dividers.").format(value))
datastruct = tuple(pieces)
return datastruct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment