From c93ddce7dfa8c01b625dfee6c80020f4c93c1985 Mon Sep 17 00:00:00 2001 From: ryan Date: Fri, 29 Mar 2024 10:33:58 -0400 Subject: [PATCH] 3/29 checkpoint --- stix2/properties.py | 56 +++++++++++++++++++++---------- stix2/test/v21/test_properties.py | 45 ++++++++++++++----------- 2 files changed, 63 insertions(+), 38 deletions(-) diff --git a/stix2/properties.py b/stix2/properties.py index 7b81bb88..c111103b 100644 --- a/stix2/properties.py +++ b/stix2/properties.py @@ -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] @@ -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: @@ -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.") diff --git a/stix2/test/v21/test_properties.py b/stix2/test/v21/test_properties.py index 4aeeb8bc..260fc7a5 100644 --- a/stix2/test/v21/test_properties.py +++ b/stix2/test/v21/test_properties.py @@ -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'