Skip to content

Commit 05c7b25

Browse files
committed
feat(profiles): ✨ detect invalid JSON in file descriptor and abort validation
Make the "File Descriptor JSON format" check (RO-Crate 1.1 and 1.2) catch JSONDecodeError explicitly: report a precise issue with the parse error message and line/column, then call context.abort_validation() so no metadata is read and downstream checks don't emit false positives.
1 parent 40f9a56 commit 05c7b25

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
# pylint: disable=invalid-name # profile filename uses digit prefix (load-order convention)
1616

17+
import json
1718
import re
1819
from typing import Any
1920
from urllib.parse import urljoin
@@ -77,6 +78,17 @@ def check(self, context: ValidationContext) -> bool:
7778
logger.debug("Checking validity of JSON file at %s", context.ro_crate.metadata)
7879
context.ro_crate.metadata.as_dict()
7980
return True
81+
except json.JSONDecodeError as e:
82+
context.result.add_issue(
83+
f'RO-Crate file descriptor "{context.rel_fd_path}" is not valid JSON: '
84+
f"{e.msg} at line {e.lineno}, column {e.colno} (char {e.pos})",
85+
self,
86+
)
87+
if logger.isEnabledFor(logging.DEBUG):
88+
logger.debug("RO-Crate file descriptor is not valid JSON", exc_info=True)
89+
# The metadata cannot be parsed: abort to avoid false positives downstream.
90+
context.abort_validation(f"file descriptor is not valid JSON: {e}")
91+
return False
8092
except Exception:
8193
context.result.add_issue(
8294
f'RO-Crate file descriptor "{context.rel_fd_path}" is not in the correct format', self

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import json
1516
import re
1617
from http import HTTPStatus
1718
from pathlib import Path
@@ -128,6 +129,17 @@ def check(self, context: ValidationContext) -> bool:
128129
logger.debug("Checking validity of JSON file at %s", context.ro_crate.metadata)
129130
context.ro_crate.metadata.as_dict()
130131
return True
132+
except json.JSONDecodeError as e:
133+
context.result.add_issue(
134+
f'RO-Crate file descriptor "{context.rel_fd_path}" is not valid JSON: '
135+
f"{e.msg} at line {e.lineno}, column {e.colno} (char {e.pos})",
136+
self,
137+
)
138+
if logger.isEnabledFor(logging.DEBUG):
139+
logger.debug("RO-Crate file descriptor is not valid JSON", exc_info=True)
140+
# The metadata cannot be parsed: abort to avoid false positives downstream.
141+
context.abort_validation(f"file descriptor is not valid JSON: {e}")
142+
return False
131143
except Exception:
132144
context.result.add_issue(
133145
f'RO-Crate file descriptor "{context.rel_fd_path}" is not in the correct format', self

0 commit comments

Comments
 (0)