Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the "instance" field in LinkML validation errors #5

Merged
merged 2 commits into from
Sep 16, 2024
Merged
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
40 changes: 37 additions & 3 deletions src/dandisets_linkml_status_tools/cli/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,47 @@
from datetime import datetime
from typing import Any
from typing import Annotated, Any

from dandi.dandiapi import VersionStatus
from linkml.validator.report import ValidationResult
from pydantic import BaseModel, Json, TypeAdapter
from pydantic import BaseModel, Json, PlainSerializer, TypeAdapter
from typing_extensions import TypedDict # Required for Python < 3.12 by Pydantic

# A `TypedDict` that has a key corresponding to each field in `ValidationResult`
# except for the `instance` field
TrimmedValidationResult = TypedDict(
"TrimmedValidationResult",
{
name: info.annotation
for name, info in ValidationResult.model_fields.items()
if name != "instance"
},
)


def trim_validation_results(
errs: list[ValidationResult],
) -> list[TrimmedValidationResult]:
"""
Trim the `ValidationResult` objects in a list to exclude their `instance` field.

:param errs: The list of `ValidationResult` objects to be trimmed.

:return: The list of `TrimmedValidationResult` objects representing the trimmed
`ValidationResult` objects.
"""
trimmed_errs = []
for err in errs:
err_as_dict = err.model_dump()
del err_as_dict["instance"]
trimmed_errs.append(err_as_dict)
return trimmed_errs


DandisetMetadataType = dict[str, Any]
PydanticValidationErrsType = list[dict[str, Any]]
LinkmlValidationErrsType = list[ValidationResult]
LinkmlValidationErrsType = Annotated[
list[ValidationResult], PlainSerializer(trim_validation_results)
]

dandiset_metadata_adapter = TypeAdapter(DandisetMetadataType)
pydantic_validation_errs_adapter = TypeAdapter(PydanticValidationErrsType)
Expand Down
Loading