summaryrefslogtreecommitdiff
path: root/src/collector/schema.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/collector/schema.py')
-rw-r--r--src/collector/schema.py57
1 files changed, 14 insertions, 43 deletions
diff --git a/src/collector/schema.py b/src/collector/schema.py
index e291f10..221990a 100644
--- a/src/collector/schema.py
+++ b/src/collector/schema.py
@@ -1,8 +1,5 @@
-from typing import List, Any, Dict
-import json
-import sys
-import traceback
-
+"""Our schema module"""
+from typing import Any, Dict
import jsonschema
# fmt:off
@@ -64,7 +61,8 @@ schema = {
]
},
{
- "required": [
+ "required":
+ [
"display_name",
"investigation_needed",
# "reliability", # TODO: reliability is required if investigation_needed = true
@@ -93,44 +91,17 @@ schema = {
"result",
],
}
-# fmt:on
-
-
-def get_index_keys() -> List[str]:
- keys: List[str] = []
- for key in schema["properties"]:
- keys.append(key)
- return keys
-def as_index_list() -> List[Dict[str, Any]]:
- index_list: List[Dict[str, Any]] = []
- for key in schema["properties"]:
- name = f"{key}-json-index"
- index = {
- "index": {
- "fields": [
- key,
- ]
- },
- "name": name,
- "type": "json",
- }
- index_list.append(index)
-
- return index_list
+def valid_schema(json_data: Dict[str, Any]) -> bool:
+ """Check if json data follows the schema.
-
-def validate_collector_data(json_blob: Dict[str, Any]) -> str:
+ :param json_data: Json object
+ :return: bool
+ """
try:
- jsonschema.validate(json_blob, schema, format_checker=jsonschema.FormatChecker())
- except jsonschema.exceptions.ValidationError as e:
- return f"Validation failed with error: {e.message}"
- return ""
-
-
-if __name__ == "__main__":
- with open(sys.argv[1]) as fd:
- json_data = json.loads(fd.read())
-
- print(validate_collector_data(json_data))
+ jsonschema.validate(json_data, schema, format_checker=jsonschema.FormatChecker())
+ except jsonschema.exceptions.ValidationError as exc:
+ print(f"Validation failed with error: {exc.message}")
+ return False
+ return True