chg: Do not load schema for event when not necessary

pull/764/head
Jakub Onderka 2021-06-28 18:21:09 +02:00
parent fa536ee41e
commit 82a6e488a3
2 changed files with 12 additions and 6 deletions

View File

@ -1396,11 +1396,8 @@ class MISPEvent(AbstractMISP):
def __init__(self, describe_types: Optional[Dict] = None, strict_validation: bool = False, **kwargs):
super().__init__(**kwargs)
if strict_validation:
schema_file = 'schema.json'
else:
schema_file = 'schema-lax.json'
self.__json_schema = self._load_json(self.resources_path / schema_file)
self.__schema_file = 'schema.json' if strict_validation else 'schema-lax.json'
if describe_types:
# This variable is used in add_attribute in order to avoid duplicating the structure
self.describe_types = describe_types
@ -1618,7 +1615,8 @@ class MISPEvent(AbstractMISP):
event.pop('Object', None)
self.from_dict(**event)
if validate:
jsonschema.validate(json.loads(self.to_json()), self.__json_schema)
json_schema = self._load_json(self.resources_path / self.__schema_file)
jsonschema.validate({"Event": self.jsonable()}, json_schema)
def __setattr__(self, name, value):
if name in ['date']:

View File

@ -48,6 +48,14 @@ class TestMISPEvent(unittest.TestCase):
del self.mispevent.uuid
self.assertEqual(self.mispevent.to_json(sort_keys=True, indent=2), json.dumps(ref_json, sort_keys=True, indent=2))
def test_loadfile_validate(self):
misp_event = MISPEvent()
misp_event.load_file('tests/mispevent_testfiles/event.json', validate=True)
def test_loadfile_validate_strict(self):
misp_event = MISPEvent(strict_validation=True)
misp_event.load_file('tests/mispevent_testfiles/event.json', validate=True)
def test_event_tag(self):
self.init_event()
self.mispevent.add_tag('bar')