Validate UUID portion of IDProperty

stix2.1
clenk 2017-04-06 13:08:48 -04:00
parent 5e4ca9e882
commit ce0de97df1
2 changed files with 10 additions and 2 deletions

View File

@ -102,9 +102,12 @@ class IDProperty(Property):
super(IDProperty, self).__init__()
def validate(self, value):
# TODO: validate GUID as well
if not value.startswith(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
def default(self):

View File

@ -63,8 +63,13 @@ def test_id_property():
idprop = IDProperty('my-type')
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')
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())