diff --git a/src/caoscrawler/validator.py b/src/caoscrawler/validator.py index 67775163af0ca778c08fb9eccdd580f26481b3a1..3cd57cd5842e3de31d59b5e4f489009a009ee1cc 100644 --- a/src/caoscrawler/validator.py +++ b/src/caoscrawler/validator.py @@ -149,14 +149,15 @@ def validate(records: list[db.Record], schemas: dict[str, dict]) -> list[tuple[b retval = [] for r in records: - if len(r.parents) != 0: + if len(r.parents) != 1: raise RuntimeError( "Schema validation is only supported if records have exactly one parent.") - if r.parents[0] not in schemas: + parname = r.parents[0].name + if parname not in schemas: raise RuntimeError( - "No schema for record type {} in schema dictionary.".format(r.parents[0])) + "No schema for record type {} in schema dictionary.".format(parname)) try: - jsonschema.validate(convert_record(r), schemas[r.parents[0]]) + jsonschema.validate(convert_record(r), schemas[parname]) retval.append((True, None)) except ValidationError as ex: retval.append((False, ex)) diff --git a/unittests/test_validation.py b/unittests/test_validation.py index 6db0674eed7b9390ac83735bb27a13c52db178b2..216b51fa06f09b93e12c6a28d0ce75e5093bd80e 100644 --- a/unittests/test_validation.py +++ b/unittests/test_validation.py @@ -51,7 +51,8 @@ def test_create_json_schema(): pobj = convert_record(r) # print(yaml.dump(pobj)) # print(yaml.dump(json[0])) - jsonschema.validate(pobj, json[0]) + assert "Dataset" in json + jsonschema.validate(pobj, json["Dataset"]) # Failing test: r = db.Record() @@ -62,7 +63,7 @@ def test_create_json_schema(): pobj = convert_record(r) with pytest.raises(ValidationError, match=".*'keywords' is a required property.*"): - jsonschema.validate(pobj, json[0]) + jsonschema.validate(pobj, json["Dataset"]) def test_validation(): @@ -83,6 +84,7 @@ def test_validation(): valres = validate([r1, r2], json) assert valres[0][0] is True - assert len(valres[0][1]) == 1 - assert valres[0][1][0] == json[0] - assert len(valres[1][1]) == 0 + assert valres[0][1] is None + assert not valres[1][0] + assert isinstance(valres[1][1], ValidationError) + assert valres[1][1].message == "'keywords' is a required property"