Skip to content

Commit 6351ad0

Browse files
authored
Merge pull request #592 from ISA-tools/deprecation_warnings
check_isa_schemas updated to new json validator. #591
2 parents 5cf4ae3 + 8767bae commit 6351ad0

32 files changed

Lines changed: 197 additions & 66 deletions

isatools/convert/isatab2cedar.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
import os
99
from os import listdir
1010
from os.path import isdir, join
11+
from pathlib import Path
1112
from uuid import uuid4
1213

13-
from jsonschema import Draft4Validator, RefResolver
14+
from jsonschema import Draft4Validator, FormatChecker
1415
from jsonschema.exceptions import ValidationError
16+
from referencing import Registry
17+
from referencing.jsonschema import DRAFT4
1518

1619
from isatools.io import isatab_parser
1720

@@ -38,11 +41,24 @@ def createCEDARjson(self, work_dir, json_dir, inv_identifier):
3841
log.info("Converting ISA to CEDAR model for {}".format(work_dir))
3942
schema_file = "investigation_template.json"
4043
with open(join(CEDAR_SCHEMA_PATH, schema_file)) as json_fp:
41-
schema = json.load(json_fp)
42-
if schema is None:
44+
investigation_schema = json.load(json_fp)
45+
if investigation_schema is None:
4346
raise IOError("Could not load schema from {}".format(join(CEDAR_SCHEMA_PATH, schema_file)))
44-
resolver = RefResolver("file://{}".format(join(CEDAR_SCHEMA_PATH, schema_file)), schema)
45-
validator = Draft4Validator(schema, resolver=resolver)
47+
48+
resources = []
49+
schemas_dir = Path(CEDAR_SCHEMA_PATH)
50+
investigation_schema_path = Path(join(CEDAR_SCHEMA_PATH, schema_file))
51+
52+
for p in sorted(schemas_dir.glob("*.json")):
53+
contents = json.loads(p.read_text(encoding="utf-8"))
54+
resource = DRAFT4.create_resource(contents)
55+
resources.append((p.resolve().as_uri(), resource))
56+
57+
registry = Registry().with_resources(resources)
58+
main_uri = investigation_schema_path.resolve().as_uri()
59+
print(registry.contents(main_uri))
60+
schema_ref = {"$ref": main_uri, "$schema": "http://json-schema.org/draft-04/schema"}
61+
validator = Draft4Validator(schema_ref, registry=registry, format_checker=FormatChecker())
4662

4763
isa_tab = isatab_parser.parse(work_dir)
4864

@@ -121,7 +137,7 @@ def createCEDARjson(self, work_dir, json_dir, inv_identifier):
121137
study_identifier = ""
122138

123139
try:
124-
validator.validate(cedar_json, schema)
140+
validator.validate(cedar_json)
125141
except ValidationError as e:
126142
error_file_name = os.path.join(json_dir, "error.log")
127143
with open(error_file_name, "w") as errorfile:

isatools/convert/isatab2json.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
import re
99
from enum import Enum
1010
from os.path import join
11+
from pathlib import Path
1112
from uuid import uuid4
1213

13-
from jsonschema import Draft4Validator, RefResolver
14+
from jsonschema import Draft202012Validator, FormatChecker
15+
from referencing import Registry
16+
from referencing.jsonschema import DRAFT202012
1417

1518
from isatools import isatab
1619
from isatools.io.isatab_parser import parse
@@ -139,10 +142,27 @@ def convert(self, work_dir):
139142

140143
# validate json
141144
with open(join(SCHEMAS_PATH, INVESTIGATION_SCHEMA)) as json_fp:
142-
schema = json.load(json_fp)
143-
resolver = RefResolver("file://" + join(SCHEMAS_PATH, INVESTIGATION_SCHEMA), schema)
144-
validator = Draft4Validator(schema, resolver=resolver)
145-
validator.validate(isa_json, schema)
145+
# investigation_schema = json.load(json_fp)
146+
# schema = DRAFT4.create_resource(investigation_schema)
147+
# registry = Registry.with_resource(investigation_schema['id'], schema)
148+
# validator = Draft4Validator(investigation_schema, registry=registry)
149+
# validator.validate(isa_json)
150+
151+
resources = []
152+
schemas_dir = Path(SCHEMAS_PATH)
153+
investigation_schema_path = Path(join(SCHEMAS_PATH, INVESTIGATION_SCHEMA))
154+
155+
for p in sorted(schemas_dir.glob("*.json")):
156+
contents = json.loads(p.read_text(encoding="utf-8"))
157+
resource = DRAFT202012.create_resource(contents)
158+
resources.append((p.resolve().as_uri(), resource))
159+
160+
registry = Registry().with_resources(resources)
161+
main_uri = investigation_schema_path.resolve().as_uri()
162+
print(registry.contents(main_uri))
163+
schema_ref = {"$ref": main_uri, "$schema": "https://json-schema.org/draft/2020-12/schema"}
164+
validator = Draft202012Validator(schema_ref, registry=registry, format_checker=FormatChecker())
165+
validator.validate(isa_json)
146166

147167
log.info("Conversion finished")
148168
return isa_json

isatools/isajson/validate.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
import os
1414
import re
1515
from io import StringIO
16+
from pathlib import Path
1617

17-
from jsonschema import Draft4Validator, RefResolver, ValidationError
18+
from jsonschema import Draft202012Validator, FormatChecker, ValidationError
19+
from referencing import Registry
20+
from referencing.jsonschema import DRAFT202012
1821

1922
from isatools.isajson.load import load
2023

@@ -547,10 +550,21 @@ def check_isa_schemas(isa_json, investigation_schema_path):
547550
"""Used for rule 0003 and 4003"""
548551
try:
549552
with open(investigation_schema_path) as fp:
550-
investigation_schema = json.load(fp)
551-
resolver = RefResolver("file://" + investigation_schema_path, investigation_schema)
552-
validator = Draft4Validator(investigation_schema, resolver=resolver)
553+
resources = []
554+
investigation_schema_path = Path(investigation_schema_path)
555+
schemas_dir = investigation_schema_path.parent
556+
557+
for p in sorted(schemas_dir.glob("*.json")):
558+
contents = json.loads(p.read_text(encoding="utf-8"))
559+
resource = DRAFT202012.create_resource(contents)
560+
resources.append((p.resolve().as_uri(), resource))
561+
562+
registry = Registry().with_resources(resources)
563+
main_uri = investigation_schema_path.resolve().as_uri()
564+
schema_ref = {"$ref": main_uri, "$schema": "https://json-schema.org/draft/2020-12/schema"}
565+
validator = Draft202012Validator(schema_ref, registry=registry, format_checker=FormatChecker())
553566
validator.validate(isa_json)
567+
554568
except ValidationError as ve:
555569
errors.append({"message": "Invalid JSON against ISA-JSON schemas", "supplemental": str(ve), "code": 3})
556570
log.fatal("(F) The JSON does not validate against the provided ISA-JSON schemas!")

isatools/net/ols.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def get_ols_ontologies():
3939
file=file,
4040
)
4141
)
42+
4243
return ontology_sources
4344

4445

@@ -47,6 +48,7 @@ def get_ols_ontology(ontology_name):
4748
ontologiesUri = OLS_API_BASE_URI + "/ontologies?size=" + str(OLS_PAGINATION_SIZE)
4849
log.debug(ontologiesUri)
4950
J = json.loads(urlopen(ontologiesUri).read().decode("utf-8"))
51+
print("EMBEDDED: ", J["_embedded"]["ontologies"])
5052
ontology_sources = []
5153
for ontology_source_json in J["_embedded"]["ontologies"]:
5254
ontology_sources.append(
@@ -57,7 +59,10 @@ def get_ols_ontology(ontology_name):
5759
file=ontology_source_json["_links"]["self"]["href"],
5860
)
5961
)
62+
print("NAME: ", ontology_name)
63+
print("SOURCES: ", [o.name for o in ontology_sources])
6064
hits = [o for o in ontology_sources if o.name == ontology_name]
65+
print("HITS: ", hits)
6166
if len(hits) == 1:
6267
return hits[0]
6368
return None

isatools/net/storage_adapter.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55
import json
66
import logging
77
import os
8-
import pathlib
98
from abc import ABCMeta, abstractmethod
109
from io import BytesIO, StringIO
10+
from os.path import join
11+
from pathlib import Path
1112
from urllib.parse import urljoin
1213
from zipfile import ZipFile
1314

1415
import requests
15-
from jsonschema import Draft4Validator, RefResolver
16+
from jsonschema import Draft4Validator, FormatChecker # RefResolver
1617
from lxml import etree
18+
from referencing import Registry
19+
from referencing.jsonschema import DRAFT4
1720

1821
log = logging.getLogger("isatools")
1922

@@ -58,8 +61,22 @@ def validate_json_against_schema(json_dict, schema_src):
5861
"""
5962
with open(schema_src) as schema_file:
6063
schema = json.load(schema_file)
61-
resolver = RefResolver(pathlib.Path(os.path.abspath(schema_src)).as_uri(), schema)
62-
validator = Draft4Validator(schema, resolver=resolver)
64+
resources = []
65+
schemas_dir = Path(schema_src)
66+
investigation_schema_path = Path(join(schema_src, schema_file))
67+
68+
for p in sorted(schemas_dir.glob("*.json")):
69+
contents = json.loads(p.read_text(encoding="utf-8"))
70+
resource = DRAFT4.create_resource(contents)
71+
resources.append((p.resolve().as_uri(), resource))
72+
73+
registry = Registry().with_resources(resources)
74+
main_uri = investigation_schema_path.resolve().as_uri()
75+
print(registry.contents(main_uri))
76+
schema_ref = {"$ref": main_uri, "$schema": "http://json-schema.org/draft-04/schema"}
77+
validator = Draft4Validator(schema_ref, registry=registry, format_checker=FormatChecker())
78+
79+
# validator = Draft4Validator(schema)
6380
return validator.validate(json_dict, schema)
6481

6582

isatools/resources/schemas/isa_model_version_1_0_schemas/core/assay_schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "https://raw.githubusercontent.com/ISA-tools/isa-api/master/isatools/resources/schemas/isa_model_version_1_0_schemas/core/assay_schema.json",
2+
"$id": "https://raw.githubusercontent.com/ISA-tools/isa-api/master/isatools/resources/schemas/isa_model_version_1_0_schemas/core/assay_schema.json",
33
"$schema": "https://json-schema.org/draft/2020-12/schema",
44
"title": "ISA Assay JSON Schema",
55
"name": "ISA Assay JSON Schema",

isatools/resources/schemas/isa_model_version_1_0_schemas/core/comment_schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "https://raw.githubusercontent.com/ISA-tools/isa-api/master/isatools/resources/schemas/isa_model_version_1_0_schemas/core/comment_schema.json",
2+
"$id": "https://raw.githubusercontent.com/ISA-tools/isa-api/master/isatools/resources/schemas/isa_model_version_1_0_schemas/core/comment_schema.json",
33
"$schema": "https://json-schema.org/draft/2020-12/schema",
44
"title": "ISA Comment schema - it corresponds to ISA Comment[] construct",
55
"name" : "ISA Comment schema",

isatools/resources/schemas/isa_model_version_1_0_schemas/core/data_schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "https://raw.githubusercontent.com/ISA-tools/isa-api/master/isatools/resources/schemas/isa_model_version_1_0_schemas/core/data_schema.json",
2+
"$id": "https://raw.githubusercontent.com/ISA-tools/isa-api/master/isatools/resources/schemas/isa_model_version_1_0_schemas/core/data_schema.json",
33
"$schema": "https://json-schema.org/draft/2020-12/schema",
44
"title": "ISA Data schema",
55
"name" : "ISA Data schema",

isatools/resources/schemas/isa_model_version_1_0_schemas/core/factor_schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "https://raw.githubusercontent.com/ISA-tools/isa-api/master/isatools/resources/schemas/isa_model_version_1_0_schemas/core/factor_schema.json",
2+
"$id": "https://raw.githubusercontent.com/ISA-tools/isa-api/master/isatools/resources/schemas/isa_model_version_1_0_schemas/core/factor_schema.json",
33
"$schema": "https://json-schema.org/draft/2020-12/schema",
44
"title": "ISA Factor schema",
55
"name": "ISA Factor schema",

isatools/resources/schemas/isa_model_version_1_0_schemas/core/factor_value_schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "https://raw.githubusercontent.com/ISA-tools/isa-api/master/isatools/resources/schemas/isa_model_version_1_0_schemas/core/factor_value_schema.json",
2+
"$id": "https://raw.githubusercontent.com/ISA-tools/isa-api/master/isatools/resources/schemas/isa_model_version_1_0_schemas/core/factor_value_schema.json",
33
"$schema": "https://json-schema.org/draft/2020-12/schema",
44
"title": "ISA Factor Value schema",
55
"name": "ISA Factor Value schema",

0 commit comments

Comments
 (0)