diff --git a/pymisp/mispevent.py b/pymisp/mispevent.py index 65e4c94..3d0fe4c 100644 --- a/pymisp/mispevent.py +++ b/pymisp/mispevent.py @@ -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']: diff --git a/tests/test_mispevent.py b/tests/test_mispevent.py index 251d9ee..08b34f5 100644 --- a/tests/test_mispevent.py +++ b/tests/test_mispevent.py @@ -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')