Skip to content

Commit

Permalink
Merge pull request dandi#32 from candleindark/enh
Browse files Browse the repository at this point in the history
Rename type alias for dandi metadata and the corresponding type adapter
  • Loading branch information
candleindark authored Dec 4, 2024
2 parents 23a0007 + 7c37523 commit ecfcc95
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/dandisets_linkml_status_tools/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from dandisets_linkml_status_tools.models import (
AssetValidationReport,
Config,
DandiMetadata,
DandisetValidationReport,
)
from dandisets_linkml_status_tools.tools import (
Expand Down Expand Up @@ -210,10 +211,10 @@ def extend_asset_validation_reports() -> None:
# JSON string read from the assets metadata file
assets_metadata_json = assets_metadata_file_path.read_text()

assets_metadata_type_adapter = TypeAdapter(list[dict[str, Any]])
assets_metadata_type_adapter = TypeAdapter(list[DandiMetadata])
try:
# Assets metadata as a list of dictionaries
assets_metadata_python: list[dict[str, Any]] = (
assets_metadata_python: list[DandiMetadata] = (
assets_metadata_type_adapter.validate_json(assets_metadata_json)
)
except ValidationError as e:
Expand Down
7 changes: 4 additions & 3 deletions src/dandisets_linkml_status_tools/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from pydantic2linkml.cli.tools import LogLevel
from typing_extensions import TypedDict # Required for Python < 3.12 by Pydantic

DandisetMetadataType: TypeAlias = dict[str, Any]
# Dandi metadata instances are always objects in JSON
DandiMetadata: TypeAlias = dict[str, Any]

PydanticValidationErrsType: TypeAlias = list[dict[str, Any]]

Expand Down Expand Up @@ -126,7 +127,7 @@ def polish_validation_results(
]


dandiset_metadata_adapter = TypeAdapter(DandisetMetadataType)
dandi_metadata_adapter = TypeAdapter(DandiMetadata)
pydantic_validation_errs_adapter = TypeAdapter(PydanticValidationErrsType)
linkml_validation_errs_adapter = TypeAdapter(LinkmlValidationErrsType)

Expand Down Expand Up @@ -226,7 +227,7 @@ def dandiset_schema_version(self) -> str:
dandiset_version_modified: datetime

# The metadata of the dandiset to be validated
dandiset_metadata: DandisetMetadataType
dandiset_metadata: DandiMetadata

# Error encountered in validation against the Pydantic dandiset metadata model
pydantic_validation_errs: Json[PydanticValidationErrsType] = []
Expand Down
11 changes: 6 additions & 5 deletions src/dandisets_linkml_status_tools/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
from yaml import dump as yaml_dump

from .models import (
DandiMetadata,
DandisetLinkmlTranslationReport,
JsonschemaValidationErrorType,
LinkmlValidationErrsType,
PydanticValidationErrsType,
ValidationReport,
dandiset_metadata_adapter,
dandi_metadata_adapter,
linkml_validation_errs_adapter,
pydantic_validation_errs_adapter,
)
Expand Down Expand Up @@ -60,11 +61,11 @@ def iter_direct_subdirs(path: Path) -> Iterable[Path]:
return (p for p in path.iterdir() if p.is_dir())


def pydantic_validate(data: dict[str, Any] | str, model: type[BaseModel]) -> str:
def pydantic_validate(data: DandiMetadata | str, model: type[BaseModel]) -> str:
"""
Validate the given data against a Pydantic model
:param data: The data, as a dict or JSON string, to be validated
:param data: The data, as a `DandiMetadata` instance or JSON string, to be validated
:param model: The Pydantic model to validate the data against
:return: A JSON string that specifies an array of errors encountered in
the validation (The JSON string returned in a case of any validation failure
Expand Down Expand Up @@ -141,7 +142,7 @@ def get_dandi_linkml_schema(cls) -> SchemaDefinition:
return cls._dandi_linkml_schema

def validate(
self, dandi_metadata: dict[str, Any], dandi_metadata_class: str
self, dandi_metadata: DandiMetadata, dandi_metadata_class: str
) -> list[ValidationResult]:
"""
Validate given DANDI metadata against a DANDI metadata model
Expand Down Expand Up @@ -401,7 +402,7 @@ def output_reports(
report_dir.mkdir(parents=True)

_write_data(
r.dandiset_metadata, dandiset_metadata_adapter, "metadata", report_dir
r.dandiset_metadata, dandi_metadata_adapter, "metadata", report_dir
)
_write_data(
r.pydantic_validation_errs,
Expand Down

0 comments on commit ecfcc95

Please sign in to comment.