Validate UUID portion of IDProperty
parent
5e4ca9e882
commit
ce0de97df1
|
@ -102,9 +102,12 @@ class IDProperty(Property):
|
||||||
super(IDProperty, self).__init__()
|
super(IDProperty, self).__init__()
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
# TODO: validate GUID as well
|
|
||||||
if not value.startswith(self.required_prefix):
|
if not value.startswith(self.required_prefix):
|
||||||
raise ValueError("must start with '{0}'.".format(self.required_prefix))
|
raise ValueError("must start with '{0}'.".format(self.required_prefix))
|
||||||
|
try:
|
||||||
|
uuid.UUID(value.split('--', 1)[1], version=4)
|
||||||
|
except Exception:
|
||||||
|
raise ValueError("must have a valid version 4 UUID after the prefix.")
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def default(self):
|
def default(self):
|
||||||
|
|
|
@ -63,8 +63,13 @@ def test_id_property():
|
||||||
idprop = IDProperty('my-type')
|
idprop = IDProperty('my-type')
|
||||||
|
|
||||||
assert idprop.validate('my-type--90aaca8a-1110-5d32-956d-ac2f34a1bd8c')
|
assert idprop.validate('my-type--90aaca8a-1110-5d32-956d-ac2f34a1bd8c')
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError) as excinfo:
|
||||||
idprop.validate('not-my-type--90aaca8a-1110-5d32-956d-ac2f34a1bd8c')
|
idprop.validate('not-my-type--90aaca8a-1110-5d32-956d-ac2f34a1bd8c')
|
||||||
|
assert str(excinfo.value) == "must start with 'my-type--'."
|
||||||
|
with pytest.raises(ValueError) as excinfo:
|
||||||
|
idprop.validate('my-type--foo')
|
||||||
|
assert str(excinfo.value) == "must have a valid version 4 UUID after the prefix."
|
||||||
|
|
||||||
assert idprop.validate(idprop.default())
|
assert idprop.validate(idprop.default())
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue