Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 3 additions & 28 deletions src/allotropy/allotrope/allotrope.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
from __future__ import annotations

import copy
from typing import Any

import jsonschema

from allotropy.allotrope.converter import unstructure
from allotropy.allotrope.schemas import get_schema_from_model
from allotropy.exceptions import (
AllotropeSerializationError,
AllotropeValidationError,
)

# Override format checker to remove "uri-reference" check, which ASM schemas fail against.
FORMAT_CHECKER = copy.deepcopy(
jsonschema.validators.Draft202012Validator.FORMAT_CHECKER
)
FORMAT_CHECKER.checkers.pop("uri-reference", None)
from allotropy.allotrope.schemas import validate_asm_schema
from allotropy.exceptions import AllotropeSerializationError


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

try:
allotrope_schema = get_schema_from_model(model)
except Exception as e:
msg = f"Failed to retrieve schema for model: {e}"
raise AllotropeSerializationError(msg) from e
validate_asm_schema(allotrope_dict)

try:
jsonschema.validators.Draft202012Validator.check_schema(
allotrope_schema, format_checker=FORMAT_CHECKER
)
validator = jsonschema.validators.Draft202012Validator(allotrope_schema)
validator.validate(allotrope_dict)
except Exception as e:
msg = f"Failed to validate allotrope model against schema: {e}"
raise AllotropeValidationError(msg) from e
return allotrope_dict
28 changes: 28 additions & 0 deletions src/allotropy/allotrope/schemas.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
from collections.abc import Mapping
import copy
import json
from pathlib import Path
from typing import Any

import jsonschema

from allotropy.allotrope.schema_parser.path_util import (
get_full_schema_path,
get_schema_path_from_asm,
get_schema_path_from_manifest,
SHARED_SCHEMAS_DEFINITIONS_PATH,
)
from allotropy.constants import DEFAULT_ENCODING
from allotropy.exceptions import AllotropeSerializationError, AllotropeValidationError

# Override format checker to remove "uri-reference" check, which ASM schemas fail against.
FORMAT_CHECKER = copy.deepcopy(
jsonschema.validators.Draft202012Validator.FORMAT_CHECKER
)
FORMAT_CHECKER.checkers.pop("uri-reference", None)


def get_shared_definitions() -> dict[str, Any]:
Expand Down Expand Up @@ -63,3 +73,21 @@ def get_schema_from_model(model: Any) -> dict[str, Any]:
msg = f"No 'manifest' or 'field_asm_manifest' found in model: {type(model)}"
raise ValueError(msg)
return get_schema_from_manifest(manifest)


def validate_asm_schema(asm_dict: dict[str, Any]) -> None:
try:
allotrope_schema = get_schema_from_asm(asm_dict)
except Exception as e:
msg = f"Failed to retrieve schema for model: {e}"
raise AllotropeSerializationError(msg) from e

try:
jsonschema.validators.Draft202012Validator.check_schema(
allotrope_schema, format_checker=FORMAT_CHECKER
)
validator = jsonschema.validators.Draft202012Validator(allotrope_schema)
validator.validate(asm_dict)
except Exception as e:
msg = f"Failed to validate allotrope model against schema: {e}"
raise AllotropeValidationError(msg) from e
Loading