Skip to content

Commit cdd6363

Browse files
committed
moving id check to validation_data function; fixing id names in data dir
1 parent 75b071e commit cdd6363

File tree

4 files changed

+12
-19
lines changed

4 files changed

+12
-19
lines changed

reproschema/jsonldutils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,15 @@ def load_file(
9999
return data
100100

101101

102-
def validate_data(data):
102+
def validate_data(data, schemaname):
103103
"""Validate an expanded jsonld document against the pydantic model.
104104
105105
Parameters
106106
----------
107107
data : dict
108108
Python dictionary containing JSONLD object
109+
schemaname : str
110+
Name of the schema (name of the file) being validated
109111
110112
Returns
111113
-------
@@ -115,14 +117,16 @@ def validate_data(data):
115117
Validation errors if any returned by pydantic
116118
117119
"""
118-
# do we need it?
119-
# kwargs = {"algorithm": "URDNA2015", "format": "application/n-quads"}
120-
# normalized = jsonld.normalize(data, kwargs)
121120
obj_type = identify_model_class(data["@type"][0])
122121
data_fixed = [fixing_old_schema(data, copy_data=True)]
123122
context = read_contextfile(CONTEXTFILE_URL)
124123
data_fixed_comp = jsonld.compact(data_fixed, context)
125124
del data_fixed_comp["@context"]
125+
if obj_type.__name__ in ["Item", "Activity", "Protocol"]:
126+
if data_fixed_comp["id"].split("/")[-1] != schemaname:
127+
raise Exception(
128+
f"Document {data['@id']} does not match the schema name {schemaname}"
129+
)
126130
conforms = False
127131
v_text = ""
128132
try:

reproschema/tests/data/activities/activity1_embed.jsonld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"@context": "../../contexts/generic",
33
"@type": "reproschema:Activity",
4-
"@id": "activity1.jsonld",
4+
"@id": "activity1_embed.jsonld",
55
"prefLabel": "Example 1",
66
"description": "Activity example 1",
77
"schemaVersion": "1.0.0-rc4",

reproschema/tests/data/protocols/protocol1_embed.jsonld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"@context": "../../contexts/generic",
33
"@type": "reproschema:Protocol",
4-
"@id": "protocol1.jsonld",
4+
"@id": "protocol1_embed.jsonld",
55
"prefLabel": {
66
"en": "Protocol1",
77
"es": "Protocol1_es"

reproschema/validate.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ def validate_dir(
9292
if stop is not None:
9393
stop_server(stop)
9494
raise ValueError(f"Empty data graph in file {name}")
95-
if data["@id"].split("/")[-1] != name.split("/")[-1]:
96-
raise Exception(
97-
f"Document {data['@id']} does not match file name {name}"
98-
)
99-
conforms, vtext = validate_data(data)
95+
conforms, vtext = validate_data(data, schemaname=Path(name).name)
10096
except (ValueError, json.JSONDecodeError):
10197
if stop is not None:
10298
stop_server(stop)
@@ -137,27 +133,20 @@ def validate(path):
137133
138134
"""
139135
if Path(path).is_dir():
140-
141136
lgr.info(f"Validating directory {path}")
142-
143137
stop, port = start_server()
144138
http_kwargs = {"port": port}
145139
started = True
146-
147140
conforms, _ = validate_dir(
148141
path, started=started, http_kwargs=http_kwargs, stop=stop
149142
)
150-
151143
stop_server(stop)
152-
153144
else:
154-
155145
if Path(path).name in FILES_TO_SKIP:
156146
lgr.info(f"Skipping file {path}")
157147
return True
158-
159148
data = load_file(path, started=False)
160-
conforms, vtext = validate_data(data)
149+
conforms, vtext = validate_data(data, schemaname=Path(path).name)
161150
if not conforms:
162151
lgr.critical(f"File {path} has validation errors.")
163152
raise ValueError(vtext)

0 commit comments

Comments
 (0)