Skip to content

Commit

Permalink
test: add schema validator with reference resolution and update tests…
Browse files Browse the repository at this point in the history
… for extensions

- Added JSON Schema validator (Draft202012Validator) with reference resolution for SCDEX core schema
- Updated test cases to use the new validator from the test module
- Added test case for new extensions (location-extensions.json, 
esponsible-recruiting.json)
  • Loading branch information
vasgat committed Nov 11, 2024
1 parent 59d52a5 commit 491f22e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
84 changes: 84 additions & 0 deletions examples/full/valid/extension-schema-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
"$schema": "../../../schema/core-schema.json",
"locations": [
{
"guid": "82d39ac8-1a12-4801-835a-85d319927548",
"location-type": "factory",
"language": "en",
"products": [
"Accessories",
"Hats"
],
"processing-types": [
"Final Product Assembly",
"Cutting",
"Embroidery",
"Finishing",
"Ironing",
"Knitting"
],
"location-identifier": "osid:CN2021250D1DTN7",
"name": "Huai An Yuan Tong Headwear Mfg. Co., Ltd.",
"coordinates": {
"latitude": 33.7862099,
"longitude": 119.2787399
},
"address": {
"street-name": "Yan Huang Avenue",
"building-number": "No.30 & 32 & 99",
"town-name": "Huaian",
"country-sub-division": "Jiangsu",
"country": "CN",
"address-line": [
"No.30 & 32 & 99 Yan Huang Avenue",
"Lian Shui Economic Developmental District",
"Huaian, Jiangsu - China"
]
},
"responsible-recruiting": {
"recruitment-fee": 200,
"employer-coverage": 150,
"initial-contact-date": "2024-01-15",
"contract-signing-date": "2024-02-01",
"first-paycheck-date": "2024-03-01"
}
}
],
"organizations": [
{
"guid": "91582fdd-6f72-47cf-a113-f68fe4e74865",
"organization-type": "retailer",
"language": "en",
"organizational-identifier": "lei:3538002LJMRZ83SU0B85"
},
{
"guid": "91582fdd-6f72-47cf-a113-f68fe4e74866",
"organization-type": "retailer",
"language": "en",
"organizational-identifier": "lei:353800ZCXKHDPY0N5218"
},
{
"guid": "91582fdd-6f72-47cf-a113-f68fe4e74867",
"organization-type": "retailer",
"language": "en",
"organizational-identifier": "lei:549300D9GZ4BMLDW5T40"
}
],
"affiliations": [
{
"from-guid": "82d39ac8-1a12-4801-835a-85d319927548",
"to-guid": "91582fdd-6f72-47cf-a113-f68fe4e74865",
"affiliation-type": "is supplier of"
},
{
"from-guid": "82d39ac8-1a12-4801-835a-85d319927548",
"to-guid": "91582fdd-6f72-47cf-a113-f68fe4e74866",
"affiliation-type": "is supplier of"
},
{
"from-guid": "82d39ac8-1a12-4801-835a-85d319927548",
"to-guid": "91582fdd-6f72-47cf-a113-f68fe4e74867",
"affiliation-type": "is supplier of"
}
]
}
10 changes: 10 additions & 0 deletions tests/scdex/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
"""
This module simply exposes the SCDEX JSON schema as a Python constant for use in testing.
It also provides a JSON schema validator with reference resolution, configured to work with the SCDEX core schema and its dependencies.
"""
from pathlib import Path
import json

from jsonschema import Draft202012Validator, RefResolver

SCDEX_SCHEMA_REPO_ROOT = Path(__file__).parent.parent.parent

with open(SCDEX_SCHEMA_REPO_ROOT.joinpath("schema", "core-schema.json"), 'r') as f:
_schema_content = f.read()

SCDEX_JSON_SCHEMA = json.loads(_schema_content)

resolver = RefResolver(
base_uri=f'{SCDEX_SCHEMA_REPO_ROOT.joinpath("schema").resolve().as_uri()}/', # base directory for schema files
referrer=SCDEX_JSON_SCHEMA
)

validator = Draft202012Validator(schema=SCDEX_JSON_SCHEMA, resolver=resolver)
6 changes: 3 additions & 3 deletions tests/tests/test_scdex_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import jsonschema
import pytest
from jsonschema import validate, Draft202012Validator
from scdex import SCDEX_JSON_SCHEMA, SCDEX_SCHEMA_REPO_ROOT
from scdex import validator, SCDEX_JSON_SCHEMA, SCDEX_SCHEMA_REPO_ROOT


examples_path = SCDEX_SCHEMA_REPO_ROOT.joinpath("examples", "full")
Expand All @@ -21,7 +21,7 @@ def test_valid_examples(path_to_validate):
with open(path_to_validate, 'r') as f:
data = f.read()
data = json.loads(data)
validate(data, SCDEX_JSON_SCHEMA, cls=Draft202012Validator)
validator.validate(data)


@pytest.mark.parametrize("path_to_validate", examples_path.glob("invalid/*.json"))
Expand All @@ -31,4 +31,4 @@ def test_invalid_examples(path_to_validate):
data = f.read()
data = json.loads(data)
with pytest.raises(jsonschema.exceptions.ValidationError):
validate(data, SCDEX_JSON_SCHEMA, cls=Draft202012Validator)
validator.validate(data)

0 comments on commit 491f22e

Please sign in to comment.