Skip to content

Commit ff2afcc

Browse files
refactor: Add schema validation function for external use (#1068)
1 parent 2987975 commit ff2afcc

2 files changed

Lines changed: 31 additions & 28 deletions

File tree

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
from __future__ import annotations
22

3-
import copy
43
from typing import Any
54

6-
import jsonschema
7-
85
from allotropy.allotrope.converter import unstructure
9-
from allotropy.allotrope.schemas import get_schema_from_model
10-
from allotropy.exceptions import (
11-
AllotropeSerializationError,
12-
AllotropeValidationError,
13-
)
14-
15-
# Override format checker to remove "uri-reference" check, which ASM schemas fail against.
16-
FORMAT_CHECKER = copy.deepcopy(
17-
jsonschema.validators.Draft202012Validator.FORMAT_CHECKER
18-
)
19-
FORMAT_CHECKER.checkers.pop("uri-reference", None)
6+
from allotropy.allotrope.schemas import validate_asm_schema
7+
from allotropy.exceptions import AllotropeSerializationError
208

219

2210
def serialize_and_validate_allotrope(model: Any) -> dict[str, Any]:
@@ -26,19 +14,6 @@ def serialize_and_validate_allotrope(model: Any) -> dict[str, Any]:
2614
msg = f"Failed to serialize allotrope model: {e}"
2715
raise AllotropeSerializationError(msg) from e
2816

29-
try:
30-
allotrope_schema = get_schema_from_model(model)
31-
except Exception as e:
32-
msg = f"Failed to retrieve schema for model: {e}"
33-
raise AllotropeSerializationError(msg) from e
17+
validate_asm_schema(allotrope_dict)
3418

35-
try:
36-
jsonschema.validators.Draft202012Validator.check_schema(
37-
allotrope_schema, format_checker=FORMAT_CHECKER
38-
)
39-
validator = jsonschema.validators.Draft202012Validator(allotrope_schema)
40-
validator.validate(allotrope_dict)
41-
except Exception as e:
42-
msg = f"Failed to validate allotrope model against schema: {e}"
43-
raise AllotropeValidationError(msg) from e
4419
return allotrope_dict

src/allotropy/allotrope/schemas.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
from collections.abc import Mapping
2+
import copy
23
import json
34
from pathlib import Path
45
from typing import Any
56

7+
import jsonschema
8+
69
from allotropy.allotrope.schema_parser.path_util import (
710
get_full_schema_path,
811
get_schema_path_from_asm,
912
get_schema_path_from_manifest,
1013
SHARED_SCHEMAS_DEFINITIONS_PATH,
1114
)
1215
from allotropy.constants import DEFAULT_ENCODING
16+
from allotropy.exceptions import AllotropeSerializationError, AllotropeValidationError
17+
18+
# Override format checker to remove "uri-reference" check, which ASM schemas fail against.
19+
FORMAT_CHECKER = copy.deepcopy(
20+
jsonschema.validators.Draft202012Validator.FORMAT_CHECKER
21+
)
22+
FORMAT_CHECKER.checkers.pop("uri-reference", None)
1323

1424

1525
def get_shared_definitions() -> dict[str, Any]:
@@ -63,3 +73,21 @@ def get_schema_from_model(model: Any) -> dict[str, Any]:
6373
msg = f"No 'manifest' or 'field_asm_manifest' found in model: {type(model)}"
6474
raise ValueError(msg)
6575
return get_schema_from_manifest(manifest)
76+
77+
78+
def validate_asm_schema(asm_dict: dict[str, Any]) -> None:
79+
try:
80+
allotrope_schema = get_schema_from_asm(asm_dict)
81+
except Exception as e:
82+
msg = f"Failed to retrieve schema for model: {e}"
83+
raise AllotropeSerializationError(msg) from e
84+
85+
try:
86+
jsonschema.validators.Draft202012Validator.check_schema(
87+
allotrope_schema, format_checker=FORMAT_CHECKER
88+
)
89+
validator = jsonschema.validators.Draft202012Validator(allotrope_schema)
90+
validator.validate(asm_dict)
91+
except Exception as e:
92+
msg = f"Failed to validate allotrope model against schema: {e}"
93+
raise AllotropeValidationError(msg) from e

0 commit comments

Comments
 (0)