mirror of https://github.com/MISP/PyTaxonomies
Add schema validator
parent
91c9966c5a
commit
af115a63c7
|
@ -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 .
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue