|
14 | 14 |
|
15 | 15 | from typing import Any |
16 | 16 |
|
17 | | -from rocrate_validator.utils import log as logging |
18 | 17 | from rocrate_validator.models import ValidationContext |
19 | 18 | from rocrate_validator.requirements.python import (PyFunctionCheck, check, |
20 | 19 | requirement) |
| 20 | +from rocrate_validator.utils import log as logging |
21 | 21 | from rocrate_validator.utils.http import HttpRequester |
22 | 22 |
|
23 | 23 | # set up logging |
@@ -339,9 +339,27 @@ def add_unexpected_key(k: str, u_keys: dict) -> None: |
339 | 339 | # If the entity is a dictionary, check each key |
340 | 340 | if isinstance(entity, dict): |
341 | 341 | 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: |
343 | 350 | 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 |
345 | 363 | if isinstance(v, (dict, list)): |
346 | 364 | self.__check_entity_keys__(v, context_keys, unexpected_keys) |
347 | 365 |
|
|
0 commit comments