Handle ID fields in a generic way.

stix2.1
Greg Back 2017-02-02 08:33:36 -06:00
parent e677167cb4
commit 67c3311672
1 changed files with 11 additions and 19 deletions

View File

@ -11,7 +11,12 @@ COMMON_PROPERTIES = {
'default': (lambda x: x._type),
'validate': (lambda x, val: val == x._type)
},
'id': {},
'id': {
'default': (lambda x: x._make_id()),
'validate': (lambda x, val: val.startswith(x._type + "--")),
'expected': (lambda x: x._type + "--"),
'error_msg': "{type} {field} values must begin with '{expected}'."
},
'created': {},
'modified': {},
}
@ -63,7 +68,8 @@ class _STIXBase(collections.Mapping):
msg = prop_metadata.get('error_msg', DEFAULT_ERROR).format(
type=class_name,
field=prop_name,
expected=prop_metadata['default'](cls),
expected=prop_metadata.get('expected',
prop_metadata['default'])(cls),
)
raise ValueError(msg)
elif prop_metadata.get('fixed'):
@ -75,13 +81,6 @@ class _STIXBase(collections.Mapping):
)
raise ValueError(msg)
id_prefix = cls._type + "--"
if not kwargs.get('id'):
kwargs['id'] = cls._make_id()
if not kwargs['id'].startswith(id_prefix):
msg = "{0} id values must begin with '{1}'."
raise ValueError(msg.format(class_name, id_prefix))
return kwargs
def __init__(self, **kwargs):
@ -124,11 +123,9 @@ class Bundle(_STIXBase):
_type = 'bundle'
_properties = {
'type': {
'default': (lambda x: x._type),
'validate': (lambda x, val: val == x._type)
},
'id': {},
# Borrow the 'type' and 'id' definitions
'type': COMMON_PROPERTIES['type'],
'id': COMMON_PROPERTIES['id'],
'spec_version': {
'fixed': "2.0",
},
@ -263,11 +260,6 @@ class Relationship(_STIXBase):
# if we won't need it?
now = datetime.datetime.now(tz=pytz.UTC)
if not kwargs.get('id'):
kwargs['id'] = 'relationship--' + str(uuid.uuid4())
if not kwargs['id'].startswith('relationship--'):
raise ValueError("Relationship id values must begin with 'relationship--'.")
if not kwargs.get('relationship_type'):
raise ValueError("Missing required field for Relationship: 'relationship_type'.")