Skip to content

Commit 56b4568

Browse files
committed
Reformat content validator and add docstrings
1 parent 8a45ee8 commit 56b4568

File tree

1 file changed

+35
-25
lines changed

1 file changed

+35
-25
lines changed

aleph_message/models/__init__.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -202,37 +202,45 @@ 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(
215+
"Field 'item_content' does not appear to be valid JSON"
216+
)
217+
json_dump = json.loads(v.json())
218+
for key, value in json_dump.items():
219+
if value != item_content[key]:
220+
self._raise_value_error(item_content, key, value)
221+
222+
@staticmethod
223+
def _raise_value_error(item_content, key, value):
224+
"""Raise a ValueError with a message that explains the content/item_content mismatch"""
225+
if isinstance(value, list):
226+
for item in value:
227+
if item not in item_content[key]:
229228
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])})"
229+
f"Field 'content.{key}' does not match 'item_content.{key}': {item} != {item_content[key]}"
231230
)
232-
return v
231+
if isinstance(value, dict):
232+
for item in value.items():
233+
if item not in item_content[key].items():
234+
raise ValueError(
235+
f"Field 'content.{key}' does not match 'item_content.{key}': {value} != {item_content[key]}"
236+
)
237+
raise ValueError(
238+
f"Field 'content.{key}' does not match 'item_content.{key}': {value} != {item_content[key]} or type mismatch ({type(value)} != {type(item_content[key])})"
239+
)
233240

234241
@validator("item_hash")
235242
def check_item_hash(cls, v: ItemHash, values) -> ItemHash:
243+
"""Check that the 'item_hash' matches the 'item_content's SHA256 hash"""
236244
item_type = values["item_type"]
237245
if item_type == ItemType.inline:
238246
item_content: str = values["item_content"]
@@ -256,13 +264,15 @@ def check_item_hash(cls, v: ItemHash, values) -> ItemHash:
256264

257265
@validator("confirmed")
258266
def check_confirmed(cls, v, values):
267+
"""Check that 'confirmed' is not True without 'confirmations'"""
259268
confirmations = values["confirmations"]
260269
if v is True and not bool(confirmations):
261270
raise ValueError("Message cannot be 'confirmed' without 'confirmations'")
262271
return v
263272

264273
@validator("time")
265274
def convert_float_to_datetime(cls, v, values):
275+
"""Converts a Unix timestamp to a datetime object"""
266276
if isinstance(v, float):
267277
v = datetime.datetime.fromtimestamp(v)
268278
assert isinstance(v, datetime.datetime)

0 commit comments

Comments
 (0)