From ae694da61cb92f8b1e29285ae8d2fd2fa900e4aa Mon Sep 17 00:00:00 2001 From: Alexander Schlemmer <a.schlemmer@indiscale.com> Date: Thu, 28 Nov 2024 13:42:20 +0100 Subject: [PATCH] FIX(validator): patches are applied recursively to match substructure of records --- src/caoscrawler/validator.py | 38 ++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/caoscrawler/validator.py b/src/caoscrawler/validator.py index 1663aa46..56104703 100644 --- a/src/caoscrawler/validator.py +++ b/src/caoscrawler/validator.py @@ -71,25 +71,20 @@ def representer_ordereddict(dumper, data): return dumper.represent_data(dict(data)) -def convert_record(record: db.Record): +def apply_schema_patches(pobj: dict): """ - Convert a record into a form suitable for validation with jsonschema. - - Uses high_level_api.convert_to_python_object - Changes applied: - properties are moved vom subitem "proeprties" to top-level. - The following keys are deleted: parents, role, name, description, metadata, properties - - Arguments: - ---------- - record: db.Record - The record that is supposed to be converted. """ - pobj = convert_to_python_object(record).serialize() - + if "properties" not in pobj: + # this is probably a file + return pobj for prop in pobj["properties"]: - pobj[prop] = pobj["properties"][prop] + if isinstance(pobj["properties"][prop], dict): + pobj[prop] = apply_schema_patches(pobj["properties"][prop]) + else: + pobj[prop] = pobj["properties"][prop] for keyd in ("parents", "role", "name", "description", "metadata", "properties"): @@ -99,6 +94,23 @@ def convert_record(record: db.Record): return pobj +def convert_record(record: db.Record): + """ + Convert a record into a form suitable for validation with jsonschema. + + Uses high_level_api.convert_to_python_object + Afterwards apply_schema_patches is called recursively to refactor the dictionary + to match the current form of the jsonschema. + + Arguments: + ---------- + record: db.Record + The record that is supposed to be converted. + """ + pobj = convert_to_python_object(record).serialize() + return apply_schema_patches(pobj) + + def validate(records: list[db.Record], schemas: list[dict]) -> list[tuple[bool, list]]: """ Validate a list of records against a list of possible JSON schemas. -- GitLab