Skip to content
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
21 changes: 14 additions & 7 deletions src/rune/runtime/base_data_class.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'''Base class for all Rune type classes'''
import logging
import importlib
import copy
import json
from typing import get_args, get_origin, Any, Literal
from typing_extensions import Self
Expand Down Expand Up @@ -177,7 +178,7 @@ def rune_deserialize(cls,
rune_data: str | dict[str, Any],
validate_model: bool = True,
check_rune_constraints: bool = True,
strict: bool = True,
strict: bool = False,
raise_validation_errors: bool = True) -> BaseModel:
# pylint: disable=line-too-long
'''Rune compliant deserialization
Expand All @@ -194,7 +195,7 @@ def rune_deserialize(cls,
deserialization. Defaults to True.

`strict (bool, optional):` Perform strict attribute validation.
Defaults to True.
Defaults to False.

`raise_validation_errors (bool, optional):` Raise an exception in
case a validation error has occurred. Defaults to True.
Expand All @@ -203,14 +204,20 @@ def rune_deserialize(cls,
`BaseModel:` The Rune model.
'''
if isinstance(rune_data, str):
rune_data = json.loads(rune_data)
rune_dict = json.loads(rune_data)
# NOTE: json.loads will not create the right atomic types
# (e.g. date, datetime, time etc) and the strict model validate
# will not attempt to convert them.
strict = False
elif not isinstance(rune_data, dict):
raise ValueError(f'rune_data is of type {type(rune_data)}, '
'alas it has to be either dict or str!')
rune_data.pop('@version', None)
rune_data.pop('@model', None)
rune_cls = cls._type_to_cls(rune_data)
model = rune_cls.model_validate(rune_data, strict=strict)
else:
rune_dict = copy.deepcopy(rune_data)
rune_dict.pop('@version', None)
rune_dict.pop('@model', None)
rune_cls = cls._type_to_cls(rune_dict)
model = rune_cls.model_validate(rune_dict, strict=strict)
model.resolve_references(ignore_dangling=False, recurse=True)
if validate_model:
model.validate_model(check_rune_constraints=check_rune_constraints,
Expand Down