Skip to content

Commit

Permalink
Validate multiple objects
Browse files Browse the repository at this point in the history
  • Loading branch information
boosterl committed Nov 13, 2023
1 parent 57afd4a commit 97e71f8
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/elody/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
class CSVParser:
top_level_fields = ["type", "filename"]
identifier_fields = ["identifiers", "identifier", "object_id", "entity_id"]
schema_mapping = {"entity": entity_schema, "mediafile": mediafile_schema}
schema_mapping = {
"entity": entity_schema,
"entities": entity_schema,
"mediafile": mediafile_schema,
"mediafiles": mediafile_schema,
}

def __init__(self, csvstring):
self.csvstring = csvstring
Expand Down Expand Up @@ -114,11 +119,15 @@ def __init__(self, csvstring, index_mapping=None, object_field_mapping=None):
if object_field_mapping:
self.object_field_mapping = object_field_mapping
self.objects = dict()
self.errors = dict()
self.__fill_objects_from_csv()

def get_entities(self):
return self.objects.get("entities", list())

def get_errors(self):
return self.errors

def get_mediafiles(self):
return self.objects.get("mediafiles", list())

Expand Down Expand Up @@ -176,5 +185,22 @@ def __fill_objects_from_csv(self):
indexed_dict[type][id]["metadata"].append(
self._get_metadata_object(key, value)
)
for metadata_type, objects in indexed_dict.items():
self.objects[metadata_type] = list(objects.values())
self.__validate_indexed_dict(indexed_dict)
for object_type, objects in indexed_dict.items():
self.objects[object_type] = list(objects.values())

def __validate_indexed_dict(self, indexed_dict):
for object_type, objects in indexed_dict.items():
error_ids = list()
for object_id, object in objects.items():
if validation_error := validate_json(
object, self.schema_mapping.get(object_type, entity_schema)
):
error_ids.append(object_id)
if object_type not in self.errors:
self.errors[object_type] = list()
self.errors[object_type].append(
f"{object_type} with index {object_id} doesn't have a valid format. {validation_error}"
)
for error_id in error_ids:
del objects[error_id]

0 comments on commit 97e71f8

Please sign in to comment.