diff options
author | Kristofer Hallin <kristofer@sunet.se> | 2022-04-12 11:03:53 +0200 |
---|---|---|
committer | Kristofer Hallin <kristofer@sunet.se> | 2022-04-12 11:03:53 +0200 |
commit | 2aebcdeca17f9b46d90f5255dd4d03caa358701e (patch) | |
tree | 1e2eba51916736496b453338626659e5c15d84fa /src/schema.py | |
parent | 951f2d1678b3b3274d81b4ea0c024b384978d9b8 (diff) |
Use the schema when creating indexes, also validate data before writing to CouchDB.
Diffstat (limited to 'src/schema.py')
-rw-r--r-- | src/schema.py | 34 |
1 files changed, 29 insertions, 5 deletions
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)) |