diff --git a/.travis.yml b/.travis.yml index 542b57d..3deeb0e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,7 @@ python: - "nightly" install: - - pip install coveralls - - pip install codecov + - pip install coveralls codecov jsonschema - pip install -r website/REQUIREMENTS.txt - pip install . diff --git a/pytaxonomies/api.py b/pytaxonomies/api.py index e699fda..b01ee89 100644 --- a/pytaxonomies/api.py +++ b/pytaxonomies/api.py @@ -14,6 +14,12 @@ try: except ImportError: HAS_REQUESTS = False +try: + import jsonschema + HAS_JSONSCHEMA = True +except ImportError: + HAS_JSONSCHEMA = False + class EncodeTaxonomies(JSONEncoder): def default(self, obj): @@ -207,6 +213,16 @@ class Taxonomies(collections.Mapping): self.description = self.manifest['description'] self.__init_taxonomies() + def validate_with_schema(self): + if not HAS_JSONSCHEMA: + raise ImportError('jsonschema is required: pip install jsonschema') + schema = os.path.join(os.path.abspath(os.path.dirname(sys.modules['pytaxonomies'].__file__)), + 'data', 'misp-taxonomies', 'schema.json') + with open(schema, 'r') as f: + loaded_schema = json.load(f) + for t in self.taxonomies.values(): + jsonschema.validate(t.taxonomy, loaded_schema) + def __load_path(self, path): with open(path, 'r') as f: return json.load(f) diff --git a/tests/tests.py b/tests/tests.py index 0d50fd9..3849f0b 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -79,6 +79,8 @@ class TestPyTaxonomies(unittest.TestCase): out = t._json() self.assertDictEqual(out, self.loaded_tax[t.name]) + def test_validate_schema(self): + self.taxonomies_offline.validate_with_schema() if __name__ == "__main__": unittest.main()