Skip to content

Commit

Permalink
Merge pull request #68 from djarecka/id_check
Browse files Browse the repository at this point in the history
adding id check to the validation
  • Loading branch information
djarecka authored Oct 31, 2024
2 parents 889fafe + 1a8304f commit aecab71
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repos:
rev: v2.3.0
hooks:
- id: codespell
args: [--toml, pyproject.toml]
args: [--toml, pyproject.toml, --skip="CHANGELOG.md"]
additional_dependencies: [tomli]

# Format TOML files
Expand Down
12 changes: 8 additions & 4 deletions reproschema/jsonldutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,15 @@ def load_file(
return data


def validate_data(data):
def validate_data(data, schemaname):
"""Validate an expanded jsonld document against the pydantic model.
Parameters
----------
data : dict
Python dictionary containing JSONLD object
schemaname : str
Name of the schema (name of the file) being validated
Returns
-------
Expand All @@ -115,14 +117,16 @@ def validate_data(data):
Validation errors if any returned by pydantic
"""
# do we need it?
# kwargs = {"algorithm": "URDNA2015", "format": "application/n-quads"}
# normalized = jsonld.normalize(data, kwargs)
obj_type = identify_model_class(data["@type"][0])
data_fixed = [fixing_old_schema(data, copy_data=True)]
context = read_contextfile(CONTEXTFILE_URL)
data_fixed_comp = jsonld.compact(data_fixed, context)
del data_fixed_comp["@context"]
if obj_type.__name__ in ["Item", "Activity", "Protocol"]:
if data_fixed_comp["id"].split("/")[-1] != schemaname:
raise Exception(
f"Document {data['@id']} does not match the schema name {schemaname}"
)
conforms = False
v_text = ""
try:
Expand Down
2 changes: 1 addition & 1 deletion reproschema/tests/data/activities/activity1_embed.jsonld
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"@context": "../../contexts/generic",
"@type": "reproschema:Activity",
"@id": "activity1.jsonld",
"@id": "activity1_embed.jsonld",
"prefLabel": "Example 1",
"description": "Activity example 1",
"schemaVersion": "1.0.0-rc4",
Expand Down
2 changes: 1 addition & 1 deletion reproschema/tests/data/protocols/protocol1_embed.jsonld
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"@context": "../../contexts/generic",
"@type": "reproschema:Protocol",
"@id": "protocol1.jsonld",
"@id": "protocol1_embed.jsonld",
"prefLabel": {
"en": "Protocol1",
"es": "Protocol1_es"
Expand Down
11 changes: 2 additions & 9 deletions reproschema/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def validate_dir(
if stop is not None:
stop_server(stop)
raise ValueError(f"Empty data graph in file {name}")
conforms, vtext = validate_data(data)
conforms, vtext = validate_data(data, schemaname=Path(name).name)
except (ValueError, json.JSONDecodeError):
if stop is not None:
stop_server(stop)
Expand Down Expand Up @@ -133,27 +133,20 @@ def validate(path):
"""
if Path(path).is_dir():

lgr.info(f"Validating directory {path}")

stop, port = start_server()
http_kwargs = {"port": port}
started = True

conforms, _ = validate_dir(
path, started=started, http_kwargs=http_kwargs, stop=stop
)

stop_server(stop)

else:

if Path(path).name in FILES_TO_SKIP:
lgr.info(f"Skipping file {path}")
return True

data = load_file(path, started=False)
conforms, vtext = validate_data(data)
conforms, vtext = validate_data(data, schemaname=Path(path).name)
if not conforms:
lgr.critical(f"File {path} has validation errors.")
raise ValueError(vtext)
Expand Down

0 comments on commit aecab71

Please sign in to comment.