Skip to content

Commit

Permalink
3/29 checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantxu1 committed Mar 29, 2024
1 parent 6c56457 commit c93ddce
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 38 deletions.
56 changes: 38 additions & 18 deletions stix2/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,17 @@ class DictionaryProperty(Property):
def __init__(self, valid_types=None, spec_version=DEFAULT_VERSION, **kwargs):
self.spec_version = spec_version

simple_type = [BinaryProperty, BooleanProperty, FloatProperty, HashesProperty, HexProperty, IDProperty, IntegerProperty, StringProperty, TimestampProperty]
if not valid_types:
valid_types = [StringProperty]
else:
for type_ in valid_types:
if isinstance(type_, ListProperty):
if type_.contained not in simple_type:
raise ValueError("Dictionary Property does not support lists of type: ", type_.contained)
elif type_ not in simple_type:
raise ValueError("Dictionary Property does not support this value's type: ", type_)

# elif not isinstance(valid_types, ListProperty):
# valid_types = [valid_types]

Expand All @@ -412,7 +421,6 @@ def clean(self, value, allow_custom=False):
except ValueError:
raise ValueError("The dictionary property must contain a dictionary")

valid_types = self.specifics
for k in dictified.keys():
if self.spec_version == '2.0':
if len(k) < 3:
Expand Down Expand Up @@ -443,24 +451,36 @@ def clean(self, value, allow_custom=False):
# if not isinstance(dictified[k], StringProperty) or not isinstance(dictified[k], IntegerProperty):
# raise ValueError("The dictionary expects values of type str or int")

simple_type = [BinaryProperty, BooleanProperty, FloatProperty, HashesProperty, HexProperty, IDProperty, IntegerProperty, StringProperty, TimestampProperty]
clear = False
# simple_type = [BinaryProperty, BooleanProperty, FloatProperty, HashesProperty, HexProperty, IDProperty, IntegerProperty, StringProperty, TimestampProperty]
# clear = False
# for type in self.valid_types:
# if type in simple_type:
# try:
# self.valid_types.clean(dict[k])
# except ValueError:
# continue
# clear = True
# elif isinstance(dictified[k], ListProperty()):
# list_type = dictified[k].contained
# if list_type in simple_type:
# for x in dictified[k]:
# list_type.clean(x)
# else:
# raise ValueError("Dictionary Property does not support lists of type: ", list_type)
# if not clear:
# raise ValueError("Dictionary Property does not support this value's type: ", self.valid_types)

clean = False
for type in self.valid_types:
if type in simple_type:
try:
self.valid_types.clean(dict[k])
except ValueError:
continue
clear = True
elif isinstance(dictified[k], ListProperty()):
list_type = dictified[k].contained
if list_type in simple_type:
for x in dictified[k]:
list_type.clean(x)
else:
raise ValueError("Dictionary Property does not support lists of type: ", list_type)
if not clear:
raise ValueError("Dictionary Property does not support this value's type: ", self.valid_types)
type_instance = type()
try:
type_instance.clean(dictified[k])
clean = True
except ValueError:
continue

if not clean:
raise ValueError("Dictionary Property does not support this value's type: ", self.valid_types)

if len(dictified) < 1:
raise ValueError("must not be empty.")
Expand Down
45 changes: 25 additions & 20 deletions stix2/test/v21/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,48 @@ def test_dictionary_property():
p.clean({})

def test_dictionary_property_values_str():
p = DictionaryProperty(valid_types=[StringProperty], spec_version='2.1', value={'x': '123'})
assert p.clean()
p = DictionaryProperty(valid_types=[StringProperty], spec_version='2.1')
result = p.clean({'x': '123'}, False)
assert result == ({'x': '123'}, False)

q = DictionaryProperty(valid_types=[StringProperty], spec_version='2.1', value={'x': 123})
q = DictionaryProperty(valid_types=[StringProperty], spec_version='2.1')
with pytest.raises(ValueError):
assert q.clean()
assert q.clean({'x': 123})

def test_dictionary_property_values_int():
p = DictionaryProperty(valid_types=[IntegerProperty], spec_version='2.1', value={'x': 123})
assert p.clean()
p = DictionaryProperty(valid_types=[IntegerProperty], spec_version='2.1')
result = p.clean({'x': 123}, False)
assert result == ({'x': 123}, False)

q = DictionaryProperty(valid_types=[IntegerProperty], spec_version='2.1', value={'x': '123'})
q = DictionaryProperty(valid_types=[IntegerProperty], spec_version='2.1')
with pytest.raises(ValueError):
assert q.clean()
assert q.clean({'x': '123'})

def test_dictionary_property_values_stringlist():
p = DictionaryProperty(valid_types=[ListProperty(StringProperty)], spec_version='2.1', value={'x': ['abc', 'def']})
assert p.clean()
p = DictionaryProperty(valid_types=[ListProperty(StringProperty)], spec_version='2.1')
result = p.clean({'x': ['abc', 'def']}, False)
assert result == ({'x': ['abc', 'def']}, False)

q = DictionaryProperty(valid_types=[ListProperty(StringProperty)], spec_version='2.1', value={'x': '123'})
q = DictionaryProperty(valid_types=[ListProperty(StringProperty)], spec_version='2.1')
with pytest.raises(ValueError):
assert q.clean()
assert q.clean({'x': '123'})

r = DictionaryProperty(valid_types=[StringProperty, IntegerProperty], spec_version='2.1', value={'x': [123, 456]})
r = DictionaryProperty(valid_types=[StringProperty, IntegerProperty], spec_version='2.1')
with pytest.raises(ValueError):
assert r.clean()
assert r.clean({'x': [123, 456]})

def test_dictionary_property_values_list():
p = DictionaryProperty(valid_types=[StringProperty, IntegerProperty], spec_version='2.1', value={'x': 123})
assert p.clean()
p = DictionaryProperty(valid_types=[StringProperty, IntegerProperty], spec_version='2.1')
result = p.clean({'x': 123}, False)
assert result == ({'x': 123}, False)

q = DictionaryProperty(valid_types=[StringProperty, IntegerProperty], spec_version='2.1', value={'x': '123'})
assert q.clean()
q = DictionaryProperty(valid_types=[StringProperty, IntegerProperty], spec_version='2.1')
result = q.clean({'x': '123'}, False)
assert result == ({'x': '123'}, False)

r = DictionaryProperty(valid_types=[StringProperty, IntegerProperty], spec_version='2.1', value={'x': ['abc', 'def']})
r = DictionaryProperty(valid_types=[StringProperty, IntegerProperty], spec_version='2.1')
with pytest.raises(ValueError):
assert r.clean()
assert r.clean({'x': ['abc', 'def']})

ID_PROP = IDProperty('my-type', spec_version="2.1")
MY_ID = 'my-type--232c9d3f-49fc-4440-bb01-607f638778e7'
Expand Down

0 comments on commit c93ddce

Please sign in to comment.