Improve error message for invalid reference types

pull/1/head
Chris Lenk 2021-06-11 13:59:48 -04:00
parent 2b064872a4
commit 7209346f0b
2 changed files with 15 additions and 7 deletions

View File

@ -590,18 +590,26 @@ class ReferenceProperty(Property):
) and obj_type not in specifics ) and obj_type not in specifics
) or obj_type in blacklist_exceptions ) or obj_type in blacklist_exceptions
if not type_ok:
raise ValueError(
"The type-specifying prefix '%s' for this property is not "
"valid" % obj_type,
)
# We need to figure out whether the referenced object is custom or # We need to figure out whether the referenced object is custom or
# not. No good way to do that at present... just check if # not. No good way to do that at present... just check if
# unregistered and for the "x-" type prefix, for now? # unregistered and for the "x-" type prefix, for now?
has_custom = not is_object(obj_type, self.spec_version) \ has_custom = not is_object(obj_type, self.spec_version) \
or obj_type.startswith("x-") or obj_type.startswith("x-")
if not type_ok:
types = self.specifics.union(self.generics)
types = ", ".join(x.name if isinstance(x, STIXTypeClass) else x for x in types)
if self.auth_type == self._WHITELIST:
msg = "not one of the valid types for this property: %s." % types
else:
msg = "one of the invalid types for this property: %s." % types
if not allow_custom and has_custom:
msg += " A custom object type may be allowed with allow_custom=True."
raise ValueError(
"The type-specifying prefix '%s' for this property is %s"
% (obj_type, msg),
)
if not allow_custom and has_custom: if not allow_custom and has_custom:
raise CustomContentError( raise CustomContentError(
"reference to custom object type: " + obj_type, "reference to custom object type: " + obj_type,

View File

@ -208,7 +208,7 @@ def test_observed_data_example_with_bad_refs():
assert excinfo.value.cls == stix2.v21.Directory assert excinfo.value.cls == stix2.v21.Directory
assert excinfo.value.prop_name == "contains_refs" assert excinfo.value.prop_name == "contains_refs"
assert "The type-specifying prefix 'monkey' for this property is not valid" in excinfo.value.reason assert "The type-specifying prefix 'monkey' for this property is not" in excinfo.value.reason
def test_observed_data_example_with_non_dictionary(): def test_observed_data_example_with_non_dictionary():