Skip to content

Commit cc0f2b5

Browse files
committed
🎨 fix model_validators to handle Union[dict, Model] values types
1 parent 8f242a6 commit cc0f2b5

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

pydid/verification_method.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,14 @@ def _allow_controller_list(cls, value: Union[str, list]):
9393

9494
@model_validator(mode="before")
9595
@classmethod
96-
def _allow_missing_controller(cls, values: dict):
96+
def _allow_missing_controller(cls, values: Union[dict, "VerificationMethod"]):
9797
"""Derive controller value from ID.
9898
9999
This validator handles a common DID Document mutation.
100100
"""
101+
if not isinstance(values, dict):
102+
values = values.__dict__
103+
101104
if "controller" not in values:
102105
if "id" not in values:
103106
raise ValueError(
@@ -113,8 +116,13 @@ def _allow_missing_controller(cls, values: dict):
113116

114117
@model_validator(mode="before")
115118
@classmethod
116-
def _method_appears_to_contain_material(cls, values: dict):
119+
def _method_appears_to_contain_material(
120+
cls, values: Union[dict, "VerificationMethod"]
121+
):
117122
"""Validate that the method appears to contain verification material."""
123+
if not isinstance(values, dict):
124+
values = values.__dict__
125+
118126
if len(values) < 4:
119127
raise ValueError(
120128
"Key material expected, found: {}".format(list(values.keys()))
@@ -123,8 +131,11 @@ def _method_appears_to_contain_material(cls, values: dict):
123131

124132
@model_validator(mode="after")
125133
@classmethod
126-
def _no_more_than_one_material_prop(cls, values: dict):
134+
def _no_more_than_one_material_prop(cls, values: Union[dict, "VerificationMethod"]):
127135
"""Validate that exactly one material property was specified on method."""
136+
if not isinstance(values, dict):
137+
values = values.__dict__
138+
128139
set_material_properties = cls.material_properties & {
129140
key for key, value in values.items() if value is not None
130141
}

0 commit comments

Comments
 (0)