Coerce boolean properties automatically

for values like "true", "F", or 1
stix2.1
clenk 2017-04-06 16:08:36 -04:00
parent ce0de97df1
commit 253989cc52
3 changed files with 36 additions and 5 deletions

View File

@ -115,12 +115,31 @@ class IDProperty(Property):
class BooleanProperty(Property):
# TODO: Consider coercing some values (like the strings "true" and "false")
def clean(self, value):
if isinstance(value, bool):
return value
trues = ['true', 't']
falses = ['false', 'f']
try:
if value.lower() in trues:
return True
if value.lower() in falses:
return False
except AttributeError:
if value == 1:
return True
if value == 0:
return False
raise ValueError("not a coercible boolean value.")
def validate(self, value):
if not isinstance(value, bool):
try:
return self.clean(value)
except ValueError:
raise ValueError("must be a boolean value.")
return value
REF_REGEX = re.compile("^[a-z][a-z-]+[a-z]--[0-9a-fA-F]{8}-[0-9a-fA-F]{4}"

View File

@ -101,7 +101,7 @@ def test_indicator_created_ref_invalid_format():
def test_indicator_revoked_invalid():
with pytest.raises(ValueError) as excinfo:
stix2.Indicator(revoked='false', **INDICATOR_KWARGS)
stix2.Indicator(revoked='no', **INDICATOR_KWARGS)
assert str(excinfo.value) == "Invalid value for Indicator 'revoked': must be a boolean value."

View File

@ -78,7 +78,19 @@ def test_boolean_property():
assert bool_prop.validate(True) is not None
assert bool_prop.validate(False) is not None
for invalid in ('true', 'false', "T", "F", 1, 0):
assert bool_prop.validate('True') is not None
assert bool_prop.validate('False') is not None
assert bool_prop.validate('true') is not None
assert bool_prop.validate('false') is not None
assert bool_prop.validate('TRUE') is not None
assert bool_prop.validate('FALSE') is not None
assert bool_prop.validate('T') is not None
assert bool_prop.validate('F') is not None
assert bool_prop.validate('t') is not None
assert bool_prop.validate('f') is not None
assert bool_prop.validate(1) is not None
assert bool_prop.validate(0) is not None
for invalid in ('abc', ['false'], {'true': 'true'}, 2, -1):
print(invalid)
with pytest.raises(ValueError):
bool_prop.validate(invalid)