Skip to content

Commit ba6f121

Browse files
committed
Migrate json schema validation to use 'referencing' instead of RefResolver
1 parent d3f0e2e commit ba6f121

2 files changed

Lines changed: 15 additions & 22 deletions

File tree

resources/app/manifests/pip_requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
flatten_json
22
jsonschema
3+
referencing
34
numpy
45
opencv-python
56
pillow

tests/asserts/validate.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import re
88
from pathlib import Path
99
import jsonschema.validators
10+
import referencing
11+
from referencing import Registry, Resource
1012
from jsonschema.validators import Draft7Validator
11-
from jsonschema import validate, RefResolver
13+
from jsonschema import validate
1214
from jsonschema import exceptions as JSONSchemaExceptions
1315

1416
schemas = {}
@@ -20,6 +22,7 @@
2022
schemaDir = os.path.join(".", "schema")
2123
schemaAbsPath = os.path.abspath(schemaDir)
2224
schemaURI = Path(schemaAbsPath).as_uri() + "/"
25+
resource_list = []
2326
for schemaFileName in os.listdir(schemaDir):
2427
with open(
2528
os.path.join(
@@ -39,7 +42,10 @@
3942
if gameKey not in schemas:
4043
schemas[gameKey] = {}
4144
try:
42-
schemas[gameKey][schemaKey] = json.load(schemaFile)
45+
schema = json.load(schemaFile)
46+
schemas[gameKey][schemaKey] = schema
47+
resource = Resource.from_contents(schema)
48+
resource_list.append((schemaFileName, resource))
4349
except json.JSONDecodeError as e:
4450
jsonFile.seek(0)
4551
errorLine = ""
@@ -62,6 +68,7 @@
6268
])
6369
bail = True
6470

71+
registry = Registry().with_resources(resource_list).crawl()
6572
print("VALIDATE")
6673

6774
# connections
@@ -113,10 +120,7 @@
113120
result = validate(
114121
instance=connectionJSON,
115122
schema=schemas["m3"]["connection"],
116-
resolver=RefResolver(
117-
base_uri=schemaURI,
118-
referrer=schemas["m3"]["connection"]
119-
)
123+
registry=registry,
120124
)
121125
except JSONSchemaExceptions.ValidationError as e:
122126
errors.append([
@@ -175,10 +179,7 @@
175179
result = validate(
176180
instance=enemiesJSON,
177181
schema=schema,
178-
resolver=RefResolver(
179-
base_uri=schemaURI,
180-
referrer=schema
181-
)
182+
registry=registry,
182183
)
183184
except JSONSchemaExceptions.ValidationError as e:
184185
errors.append([
@@ -197,10 +198,7 @@
197198
print(" REGIONS")
198199
room_validator = Draft7Validator(
199200
schema=schemas["m3"]["room"],
200-
resolver=RefResolver(
201-
base_uri=schemaURI,
202-
referrer=schemas["m3"]["room"]
203-
)
201+
registry=registry,
204202
)
205203
for region in os.listdir(os.path.join(".", "region")):
206204
if os.path.isdir(os.path.join(".", "region", region)):
@@ -304,10 +302,7 @@
304302
result = validate(
305303
instance=weaponsJSON,
306304
schema=schemas["m3"]["weapons"],
307-
resolver=RefResolver(
308-
base_uri=schemaURI,
309-
referrer=schemas["m3"]["weapons"]
310-
)
305+
registry=registry,
311306
)
312307
except JSONSchemaExceptions.ValidationError as e:
313308
errors.append([
@@ -363,10 +358,7 @@
363358
result = validate(
364359
instance=fileJSON,
365360
schema=schemas["m3"][rootType],
366-
resolver=RefResolver(
367-
base_uri=schemaURI,
368-
referrer=schemas["m3"][rootType]
369-
)
361+
registry=registry,
370362
)
371363
except JSONSchemaExceptions.ValidationError as e:
372364
errors.append([

0 commit comments

Comments
 (0)