Skip to content
Snippets Groups Projects
Commit 2cceed88 authored by Alexander Schlemmer's avatar Alexander Schlemmer
Browse files

ENH(scanner): validation function for json schemas

parent f291fbf4
No related branches found
No related tags found
2 merge requests!217TST: Make NamedTemporaryFiles Windows-compatible,!201Validator that checks created records using a json schema
Pipeline #58149 passed
...@@ -28,14 +28,12 @@ This module contains functions to validate the output of a scanner run with a ...@@ -28,14 +28,12 @@ This module contains functions to validate the output of a scanner run with a
json schema. json schema.
""" """
import json
import jsonschema import jsonschema
import linkahead as db import linkahead as db
# from caosadvancedtools.models.parser import parse_model_from_string # from caosadvancedtools.models.parser import parse_model_from_string
from caosadvancedtools.json_schema_exporter import recordtype_to_json_schema from caosadvancedtools.json_schema_exporter import recordtype_to_json_schema
from caosadvancedtools.models.parser import parse_model_from_yaml from caosadvancedtools.models.parser import parse_model_from_yaml
from jsonschema import ValidationError
from linkahead.high_level_api import convert_to_python_object from linkahead.high_level_api import convert_to_python_object
from caoscrawler import scanner from caoscrawler import scanner
...@@ -101,9 +99,12 @@ def convert_record(record: db.Record): ...@@ -101,9 +99,12 @@ def convert_record(record: db.Record):
return pobj return pobj
def validate(records: list[db.Record], schema: dict) -> tuple[list, list]: def validate(records: list[db.Record], schemas: list[dict]) -> tuple[list, list]:
""" """
Validate a list of records against a JSON schema. Validate a list of records against a list of possible JSON schemas.
It is tried to validate each schema from the list of schemas. If none of them validates
without error, it is assumed that it does not match at all.
Arguments: Arguments:
---------- ----------
...@@ -111,8 +112,8 @@ def validate(records: list[db.Record], schema: dict) -> tuple[list, list]: ...@@ -111,8 +112,8 @@ def validate(records: list[db.Record], schema: dict) -> tuple[list, list]:
records: list[db.Record] records: list[db.Record]
List of records that will be validated. List of records that will be validated.
schema: dict schemas: list[dict]
A JSON schema generated using `load_json_schema_from_datamodel_yaml`. A list of JSON schemas generated using `load_json_schema_from_datamodel_yaml`.
Returns: Returns:
-------- --------
...@@ -120,7 +121,16 @@ def validate(records: list[db.Record], schema: dict) -> tuple[list, list]: ...@@ -120,7 +121,16 @@ def validate(records: list[db.Record], schema: dict) -> tuple[list, list]:
- Index 0: A list of boolean values, one for each record in `records` determining whether - Index 0: A list of boolean values, one for each record in `records` determining whether
the validation was successful. the validation was successful.
- Index 1: A list of ValidationErrors (in case of insuccesful validation) or None if - Index 1: A list of schemas matching the record at this position of the list `records`.
the validation was successful.
""" """
retval = []
for r in records:
matching_schemas = []
for schema in schemas:
try:
jsonschema.validate(convert_record(r), schema)
matching_schemas.append(schema)
except ValidationError:
pass pass
retval.append((len(matching_schemas) > 0, matching_schemas))
return retval
...@@ -34,7 +34,8 @@ import linkahead as db ...@@ -34,7 +34,8 @@ import linkahead as db
import pytest import pytest
import yaml import yaml
from caoscrawler.validator import (convert_record, from caoscrawler.validator import (convert_record,
load_json_schema_from_datamodel_yaml) load_json_schema_from_datamodel_yaml,
validate)
from jsonschema import ValidationError from jsonschema import ValidationError
UNITTESTDIR = Path(__file__).parent UNITTESTDIR = Path(__file__).parent
...@@ -62,3 +63,26 @@ def test_create_json_schema(): ...@@ -62,3 +63,26 @@ def test_create_json_schema():
with pytest.raises(ValidationError, match=".*'keywords' is a required property.*"): with pytest.raises(ValidationError, match=".*'keywords' is a required property.*"):
jsonschema.validate(pobj, json[0]) jsonschema.validate(pobj, json[0])
def test_validation():
"""
Test for the main validation API function `validate`
"""
json = load_json_schema_from_datamodel_yaml(
join(UNITTESTDIR, "datamodels", "datamodel.yaml"))
r1 = db.Record()
r1.add_parent(name="Dataset")
r1.add_property(name="keywords", value="jakdlfjakdf")
r1.add_property(name="dateModified", value="2024-11-16")
r2 = db.Record()
r2.add_parent(name="Dataset")
r2.add_property(name="keywordss", value="jakdlfjakdf")
r2.add_property(name="dateModified", value="2024-11-16")
valres = validate([r1, r2], json)
assert valres[0][0]
assert len(valres[0][1]) == 1
assert valres[0][1][0] == json[0]
assert len(valres[1][1]) == 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment