Skip to content

Commit 9bd0cc6

Browse files
committed
Reformat content validator and add docstrings
1 parent 8a45ee8 commit 9bd0cc6

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

aleph_message/models/__init__.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -202,37 +202,43 @@ def check_item_content(cls, v: Optional[str], values) -> Optional[str]:
202202
return v
203203

204204
@validator("content")
205-
def check_content(cls, v, values):
205+
def check_content(self, v, values):
206+
"""Check that the content matches the serialized item_content"""
206207
item_type = values["item_type"]
207-
if item_type == ItemType.inline:
208-
try:
209-
item_content = json.loads(values["item_content"])
210-
except JSONDecodeError:
211-
raise ValueError(
212-
"Field 'item_content' does not appear to be valid JSON"
213-
)
214-
json_dump = json.loads(v.json())
215-
for key, value in json_dump.items():
216-
if value != item_content[key]:
217-
if isinstance(value, list):
218-
for item in value:
219-
if item not in item_content[key]:
220-
raise ValueError(
221-
f"Field 'content.{key}' does not match 'item_content.{key}': {item} != {item_content[key]}"
222-
)
223-
if isinstance(value, dict):
224-
for item in value.items():
225-
if item not in item_content[key].items():
226-
raise ValueError(
227-
f"Field 'content.{key}' does not match 'item_content.{key}': {value} != {item_content[key]}"
228-
)
208+
if item_type != ItemType.inline:
209+
return v
210+
211+
try:
212+
item_content = json.loads(values["item_content"])
213+
except JSONDecodeError:
214+
raise ValueError("Field 'item_content' does not appear to be valid JSON")
215+
json_dump = json.loads(v.json())
216+
for key, value in json_dump.items():
217+
if value != item_content[key]:
218+
self._raise_value_error(item_content, key, value)
219+
220+
@staticmethod
221+
def _raise_value_error(item_content, key, value):
222+
"""Raise a ValueError with a message that explains the content/item_content mismatch"""
223+
if isinstance(value, list):
224+
for item in value:
225+
if item not in item_content[key]:
229226
raise ValueError(
230-
f"Field 'content.{key}' does not match 'item_content.{key}': {value} != {item_content[key]} or type mismatch ({type(value)} != {type(item_content[key])})"
227+
f"Field 'content.{key}' does not match 'item_content.{key}': {item} != {item_content[key]}"
231228
)
232-
return v
229+
if isinstance(value, dict):
230+
for item in value.items():
231+
if item not in item_content[key].items():
232+
raise ValueError(
233+
f"Field 'content.{key}' does not match 'item_content.{key}': {value} != {item_content[key]}"
234+
)
235+
raise ValueError(
236+
f"Field 'content.{key}' does not match 'item_content.{key}': {value} != {item_content[key]} or type mismatch ({type(value)} != {type(item_content[key])})"
237+
)
233238

234239
@validator("item_hash")
235240
def check_item_hash(cls, v: ItemHash, values) -> ItemHash:
241+
"""Check that the 'item_hash' matches the 'item_content's SHA256 hash"""
236242
item_type = values["item_type"]
237243
if item_type == ItemType.inline:
238244
item_content: str = values["item_content"]
@@ -256,13 +262,15 @@ def check_item_hash(cls, v: ItemHash, values) -> ItemHash:
256262

257263
@validator("confirmed")
258264
def check_confirmed(cls, v, values):
265+
"""Check that 'confirmed' is not True without 'confirmations'"""
259266
confirmations = values["confirmations"]
260267
if v is True and not bool(confirmations):
261268
raise ValueError("Message cannot be 'confirmed' without 'confirmations'")
262269
return v
263270

264271
@validator("time")
265272
def convert_float_to_datetime(cls, v, values):
273+
"""Converts a Unix timestamp to a datetime object"""
266274
if isinstance(v, float):
267275
v = datetime.datetime.fromtimestamp(v)
268276
assert isinstance(v, datetime.datetime)

0 commit comments

Comments
 (0)