Skip to content

Commit 635c86b

Browse files
authored
Merge pull request crs4#155 from kikkomep/fix/issue-128
feat: allow context-prefixed terms in validation
2 parents 4b45368 + 45a7017 commit 635c86b

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

rocrate_validator/profiles/ro-crate/must/0_file_descriptor_format.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
from typing import Any
1616

17-
from rocrate_validator.utils import log as logging
1817
from rocrate_validator.models import ValidationContext
1918
from rocrate_validator.requirements.python import (PyFunctionCheck, check,
2019
requirement)
20+
from rocrate_validator.utils import log as logging
2121
from rocrate_validator.utils.http import HttpRequester
2222

2323
# set up logging
@@ -339,9 +339,27 @@ def add_unexpected_key(k: str, u_keys: dict) -> None:
339339
# If the entity is a dictionary, check each key
340340
if isinstance(entity, dict):
341341
for k, v in entity.items():
342-
if k not in context_keys and k not in SKIP_KEYS:
342+
# If the key is in the skip keys, skip it
343+
if k in SKIP_KEYS:
344+
logger.debug(f"Key {k} is a reserved JSON-LD keyword, skipping")
345+
346+
# If the key is not in the context keys,
347+
# it can be used in compacted format only if it is a valid compact IRI
348+
# with a prefix that is in the context
349+
elif k not in context_keys:
343350
logger.debug(f"Key {k} not in context keys")
344-
add_unexpected_key(k, unexpected_keys)
351+
352+
# Try to get the prefix of the compact IRI, if it has one
353+
prefix = k.split(":", 1)[0] if ":" in k else None
354+
logger.debug(f"Checking prefix {prefix} of key {k}")
355+
# If the key does not have a prefix (no colon) or the prefix is not in the context keys,
356+
# it cannot be used as a key in compacted format
357+
if prefix is None or prefix not in context_keys:
358+
logger.debug(
359+
f"Key {k} does not have a valid prefix in context keys, adding to unexpected keys")
360+
add_unexpected_key(k, unexpected_keys)
361+
362+
# If the value is a dictionary or a list, check its keys recursively
345363
if isinstance(v, (dict, list)):
346364
self.__check_entity_keys__(v, context_keys, unexpected_keys)
347365

tests/data/crates/valid/rocrate-with-custom-terms/ro-crate-metadata.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
"@context": [
33
"https://w3id.org/ro/crate/1.1/context",
44
"https://w3id.org/ro/terms/workflow-run/context",
5-
{ "myCustomTerm": "https://example.org#myCustomTerm" }
5+
{
6+
"myCustomTerm": "https://example.org#myCustomTerm",
7+
"rdfs": "http://www.w3.org/2000/01/rdf-schema#"
8+
}
69
],
710
"@graph": [
811
{
@@ -14,7 +17,9 @@
1417
"about": {
1518
"@id": "./"
1619
},
17-
"myCustomTerm": "custom term value"
20+
"myCustomTerm": "custom term value",
21+
"rdfs:label": "RO-Crate with custom terms",
22+
"rdfs:comment": "This is a comment using a term from the rdfs namespace"
1823
},
1924
{
2025
"@id": "./",

0 commit comments

Comments
 (0)