diff --git a/stix2/properties.py b/stix2/properties.py index da77278..9312e84 100644 --- a/stix2/properties.py +++ b/stix2/properties.py @@ -151,6 +151,15 @@ class IDProperty(Property): return self.required_prefix + str(uuid.uuid4()) +class IntegerProperty(Property): + + def clean(self, value): + try: + return int(value) + except Exception: + raise ValueError("must be an integer.") + + class BooleanProperty(Property): def clean(self, value): diff --git a/stix2/test/test_properties.py b/stix2/test/test_properties.py index adcecda..7e9dc13 100644 --- a/stix2/test/test_properties.py +++ b/stix2/test/test_properties.py @@ -2,7 +2,8 @@ import pytest from stix2.properties import (Property, BooleanProperty, ListProperty, StringProperty, TypeProperty, IDProperty, - ReferenceProperty, TimestampProperty) + IntegerProperty, ReferenceProperty, + TimestampProperty) from .constants import FAKE_TIME @@ -91,6 +92,27 @@ def test_id_property(): assert idprop.clean(idprop.default()) +@pytest.mark.parametrize("value", [ + 2, + -1, + 3.14, + False, +]) +def test_integer_property_valid(value): + int_prop = IntegerProperty() + assert int_prop.clean(value) is not None + + +@pytest.mark.parametrize("value", [ + "something", + StringProperty(), +]) +def test_integer_property_invalid(value): + int_prop = IntegerProperty() + with pytest.raises(ValueError): + int_prop.clean(value) + + @pytest.mark.parametrize("value", [ True, False,