remove leading - from type name re

master
Rich Piazza 2020-03-20 11:56:09 -04:00
parent 6e4151aeeb
commit 2c4e47de56
3 changed files with 18 additions and 5 deletions

View File

@ -10,7 +10,7 @@ import stix2
from .base import _Observable, _STIXBase from .base import _Observable, _STIXBase
from .exceptions import ParseError from .exceptions import ParseError
from .markings import _MarkingsMixin from .markings import _MarkingsMixin
from .utils import PREFIX_21_REGEX, TYPE_21_REGEX, TYPE_REGEX, _get_dict from .utils import PREFIX_21_REGEX, TYPE_21_REGEX, TYPE_REGEX, EXT_21_REGEX, _get_dict
STIX2_OBJ_MAPS = {} STIX2_OBJ_MAPS = {}
@ -319,7 +319,7 @@ def _register_observable_extension(
ext_type, ext_type,
) )
else: # 2.1+ else: # 2.1+
if not re.match(TYPE_21_REGEX, ext_type): if not re.match(EXT_21_REGEX, ext_type):
raise ValueError( raise ValueError(
"Invalid extension type name '%s': must only contain the " "Invalid extension type name '%s': must only contain the "
"characters a-z (lowercase ASCII), 0-9, hyphen (-), " "characters a-z (lowercase ASCII), 0-9, hyphen (-), "

View File

@ -58,6 +58,19 @@ def test_identity_custom_property():
) )
assert "must begin with an alpha character." in str(excinfo.value) assert "must begin with an alpha character." in str(excinfo.value)
with pytest.raises(stix2.exceptions.InvalidValueError) as excinfo:
stix2.v21.Identity(
id=IDENTITY_ID,
created="2015-12-21T19:59:11Z",
modified="2015-12-21T19:59:11Z",
name="John Smith",
identity_class="individual",
custom_properties={
"_foo": "bar",
},
)
assert "must begin with an alpha character." in str(excinfo.value)
identity = stix2.v21.Identity( identity = stix2.v21.Identity(
id=IDENTITY_ID, id=IDENTITY_ID,
created="2015-12-21T19:59:11Z", created="2015-12-21T19:59:11Z",
@ -210,7 +223,7 @@ def test_invalid_custom_property_in_observed_data():
x_foo='bar', x_foo='bar',
) )
assert "must begin with an alpha character." in str(excinfo.value) assert "must begin with an alpha character." in str(excinfo.value)
def test_custom_property_object_in_observable_extension(): def test_custom_property_object_in_observable_extension():

View File

@ -27,8 +27,8 @@ NOW = object()
STIX_UNMOD_PROPERTIES = ['created', 'created_by_ref', 'id', 'type'] STIX_UNMOD_PROPERTIES = ['created', 'created_by_ref', 'id', 'type']
TYPE_REGEX = re.compile(r'^\-?[a-z0-9]+(-[a-z0-9]+)*\-?$') TYPE_REGEX = re.compile(r'^\-?[a-z0-9]+(-[a-z0-9]+)*\-?$')
TYPE_21_REGEX = re.compile(r'^\-?([a-z][a-z0-9]*)+(-[a-z0-9]+)*\-?$') TYPE_21_REGEX = re.compile(r'([a-z][a-z0-9]*)+(-[a-z0-9]+)*\-?$')
EXT_21_REGEX = re.compile(r'^\-?([a-z][a-z0-9]*)+(-[a-z0-9]+)*\-ext$') EXT_21_REGEX = re.compile(r'([a-z][a-z0-9]*)+(-[a-z0-9]+)*\-ext$')
PREFIX_21_REGEX = re.compile(r'^[a-z].*') PREFIX_21_REGEX = re.compile(r'^[a-z].*')