From 2aebcdeca17f9b46d90f5255dd4d03caa358701e Mon Sep 17 00:00:00 2001 From: Kristofer Hallin Date: Tue, 12 Apr 2022 11:03:53 +0200 Subject: Use the schema when creating indexes, also validate data before writing to CouchDB. --- src/schema.py | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'src/schema.py') diff --git a/src/schema.py b/src/schema.py index f92a2ea..9bdf130 100644 --- a/src/schema.py +++ b/src/schema.py @@ -1,5 +1,6 @@ import json import sys +import traceback import jsonschema @@ -94,18 +95,41 @@ schema = { # fmt:on +def get_index_keys(): + keys = list() + for key in schema["properties"]: + keys.append(key) + return keys + + +def as_index_list(): + index_list = list() + 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 validate_collector_data(json_blob): try: jsonschema.validate(json_blob, schema) except jsonschema.exceptions.ValidationError as e: - print(f"Validation failed with error: {e}") - return False - - return True + 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()) - validate_collector_data(json_data) + print(validate_collector_data(json_data)) -- cgit v1.1