Skip to content

Commit 2946f29

Browse files
committed
Add a base validation layer to CBORSerializable
1 parent ae2c905 commit 2946f29

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

pycardano/exception.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class InvalidAddressInputException(PyCardanoException):
1414
pass
1515

1616

17+
class InvalidDataException(PyCardanoException):
18+
pass
19+
20+
1721
class InvalidArgumentException(PyCardanoException):
1822
pass
1923

pycardano/serialization.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def default_encoder(
9393
encoder.encode(item)
9494
encoder.write(b"\xff")
9595
else:
96-
encoder.encode(value.to_primitive())
96+
encoder.encode(value.to_validated_primitive())
9797

9898

9999
@typechecked
@@ -126,7 +126,7 @@ def to_shallow_primitive(self) -> Primitive:
126126
:const:`Primitive`: A CBOR primitive.
127127
128128
Raises:
129-
:class:`pycardano.exception.SerializeException`: When the object could not be converted to CBOR primitive
129+
SerializeException: When the object could not be converted to CBOR primitive
130130
types.
131131
"""
132132
raise NotImplementedError(
@@ -140,7 +140,7 @@ def to_primitive(self) -> Primitive:
140140
:const:`Primitive`: A CBOR primitive.
141141
142142
Raises:
143-
:class:`pycardano.exception.SerializeException`: When the object or its elements could not be converted to
143+
SerializeException: When the object or its elements could not be converted to
144144
CBOR primitive types.
145145
"""
146146
result = self.to_shallow_primitive()
@@ -189,6 +189,28 @@ def _dfs(value):
189189

190190
return _dfs(result)
191191

192+
def validate(self):
193+
"""Validate the data stored in the current instance. Defaults to always pass.
194+
195+
Raises:
196+
InvalidDataException: When the data is invalid.
197+
"""
198+
pass
199+
200+
def to_validated_primitive(self) -> Primitive:
201+
"""Convert the instance and its elements to CBOR primitives recursively with data validated by :meth:`validate`
202+
method.
203+
204+
Returns:
205+
:const:`Primitive`: A CBOR primitive.
206+
207+
Raises:
208+
SerializeException: When the object or its elements could not be converted to
209+
CBOR primitive types.
210+
"""
211+
self.validate()
212+
return self.to_primitive()
213+
192214
@classmethod
193215
def from_primitive(cls: CBORBase, value: Primitive) -> CBORBase:
194216
"""Turn a CBOR primitive to its original class type.
@@ -201,7 +223,7 @@ def from_primitive(cls: CBORBase, value: Primitive) -> CBORBase:
201223
CBORBase: A CBOR serializable object.
202224
203225
Raises:
204-
:class:`pycardano.exception.DeserializeException`: When the object could not be restored from primitives.
226+
DeserializeException: When the object could not be restored from primitives.
205227
"""
206228
raise NotImplementedError(
207229
f"'from_primitive()' is not implemented by {cls.__name__}."
@@ -427,7 +449,7 @@ def to_shallow_primitive(self) -> List[Primitive]:
427449
:const:`Primitive`: A CBOR primitive.
428450
429451
Raises:
430-
:class:`pycardano.exception.SerializeException`: When the object could not be converted to CBOR primitive
452+
SerializeException: When the object could not be converted to CBOR primitive
431453
types.
432454
"""
433455
primitives = []
@@ -450,7 +472,7 @@ def from_primitive(cls: ArrayBase, values: List[Primitive]) -> ArrayBase:
450472
:const:`ArrayBase`: Restored object.
451473
452474
Raises:
453-
:class:`pycardano.exception.DeserializeException`: When the object could not be restored from primitives.
475+
DeserializeException: When the object could not be restored from primitives.
454476
"""
455477
all_fields = [f for f in fields(cls) if f.init]
456478
if type(values) != list:
@@ -678,7 +700,7 @@ def from_primitive(cls: DictBase, value: dict) -> DictBase:
678700
:const:`DictBase`: Restored object.
679701
680702
Raises:
681-
:class:`pycardano.exception.DeserializeException`: When the object could not be restored from primitives.
703+
DeserializeException: When the object could not be restored from primitives.
682704
"""
683705
if not value:
684706
raise DeserializeException(f"Cannot accept empty value {value}.")

0 commit comments

Comments
 (0)