Skip to content

Commit ec02130

Browse files
committed
implement version checks
1 parent 1984245 commit ec02130

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright (c) 2024-2025 CRS4
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typing import Any
16+
import re
17+
18+
import rocrate_validator.log as logging
19+
from rocrate_validator.models import Severity, ValidationContext
20+
from rocrate_validator.requirements.python import (PyFunctionCheck, check,
21+
requirement)
22+
from rocrate_validator.utils import HttpRequester
23+
24+
# set up logging
25+
logger = logging.getLogger(__name__)
26+
27+
28+
@requirement(name="RO-Crate context version")
29+
class FileDescriptorExistence(PyFunctionCheck):
30+
"""The RO-Crate metadata file MUST include the RO-Crate context version 1.2 (or later minor version) in `@context`"""
31+
32+
@check(name="RO-Crate context version", severity=Severity.REQUIRED)
33+
def test_existence(self, context: ValidationContext) -> bool:
34+
"""
35+
The RO-Crate metadata file MUST include the RO-Crate context version 1.2 (or later minor version) in `@context`
36+
"""
37+
try:
38+
json_dict = context.ro_crate.metadata.as_dict()
39+
context_value = json_dict["@context"]
40+
pattern = re.compile(r"https://w3id\.org/ro/crate/1\.[2-9](-DRAFT)?/context")
41+
passed = True
42+
if isinstance(context_value, list):
43+
if not any(pattern.match(item) for item in context_value if isinstance(item, str)):
44+
passed = False
45+
else:
46+
if not pattern.match(context_value):
47+
passed = False
48+
if not passed:
49+
context.result.add_issue(
50+
f"The RO-Crate metadata file MUST include the RO-Crate context "
51+
"version 1.2 (or later minor version) in `@context`", self)
52+
return passed
53+
54+
except Exception as e:
55+
if logger.isEnabledFor(logging.DEBUG):
56+
logger.exception(e)
57+
return True
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright (c) 2025 eScience Lab, The University of Manchester
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
@prefix ro-crate: <https://github.com/crs4/rocrate-validator/profiles/ro-crate/> .
16+
@prefix five-safes-crate: <https://github.com/eScienceLab/rocrate-validator/profiles/five-safes-crate/> .
17+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
18+
@prefix schema: <http://schema.org/> .
19+
@prefix purl: <http://purl.org/dc/terms/> .
20+
@prefix sh: <http://www.w3.org/ns/shacl#> .
21+
@prefix validator: <https://github.com/crs4/rocrate-validator/> .
22+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
23+
@prefix dct: <http://purl.org/dc/terms/> .
24+
25+
five-safes-crate:MetadataFileDescriptorProperties a sh:NodeShape ;
26+
sh:name "RO-Crate conforms to 1.2 or later minor version" ;
27+
sh:description """The RO-Crate metadata file descriptor MUST have a `conformsTo` property with RO-Crate specification version 1.2 or later minor version""";
28+
sh:targetClass ro-crate:ROCrateMetadataFileDescriptor ;
29+
sh:property [
30+
a sh:PropertyShape ;
31+
sh:name "RO-Crate conforms to 1.2 or later minor version" ;
32+
sh:description "The RO-Crate metadata file descriptor MUST have a `conformsTo` property with RO-Crate specification version 1.2 or later minor version" ;
33+
sh:minCount 1 ;
34+
sh:nodeKind sh:IRI ;
35+
sh:path dct:conformsTo ;
36+
sh:pattern "https://w3id\\.org/ro/crate/(1\\.[2-9](-DRAFT)?)" ;
37+
sh:severity sh:Violation;
38+
sh:message "The RO-Crate metadata file descriptor MUST have a `conformsTo` property with RO-Crate specification version 1.2 or later minor version" ;
39+
] .

0 commit comments

Comments
 (0)