Skip to content

Commit

Permalink
feat: add model validator to remove "@context" key in Asset and `…
Browse files Browse the repository at this point in the history
…Dandiset`

This allows validation of data instances of `Asset` and
`Dandiset` to contain the `"@context"` key
  • Loading branch information
candleindark committed Nov 15, 2024
1 parent 9d068b9 commit 05e6307
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions dandischema/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ def diff_models(model1: M, model2: M) -> None:
print(f"{field} is different")


def get_dict_without_context(d: Any) -> Any:
"""
If a given object is a dictionary, return a copy of it without the
`@context` key. Otherwise, return the input object as is.
:param d: The given object
:return: If the object is a dictionary, a copy of it without the `@context` key;
otherwise, the input object as is.
"""
if isinstance(d, dict):
return {k: v for k, v in d.items() if k != "@context"}
return d

Check warning on line 90 in dandischema/models.py

View check run for this annotation

Codecov / codecov/patch

dandischema/models.py#L90

Added line #L90 was not covered by tests


class AccessType(Enum):
"""An enumeration of access status options"""

Expand Down Expand Up @@ -1683,6 +1697,12 @@ def contributor_musthave_contact(
"nskey": "dandi",
}

# Model validator to remove the `"@context"` key from data instance before
# "base" validation is performed.
_remove_context_key = model_validator(mode="before")(
staticmethod(get_dict_without_context)
)


class BareAsset(CommonModel):
"""Metadata used to describe an asset anywhere (local or server).
Expand Down Expand Up @@ -1815,6 +1835,12 @@ class Asset(BareAsset):
json_schema_extra={"readOnly": True, "nskey": "schema"}
)

# Model validator to remove the `"@context"` key from data instance before
# "base" validation is performed.
_remove_context_key = model_validator(mode="before")(
staticmethod(get_dict_without_context)
)


class Publishable(DandiBaseModel):
publishedBy: Union[AnyHttpUrl, PublishActivity] = Field(
Expand Down

0 comments on commit 05e6307

Please sign in to comment.