@@ -93,7 +93,7 @@ def default_encoder(
93
93
encoder .encode (item )
94
94
encoder .write (b"\xff " )
95
95
else :
96
- encoder .encode (value .to_primitive ())
96
+ encoder .encode (value .to_validated_primitive ())
97
97
98
98
99
99
@typechecked
@@ -126,7 +126,7 @@ def to_shallow_primitive(self) -> Primitive:
126
126
:const:`Primitive`: A CBOR primitive.
127
127
128
128
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
130
130
types.
131
131
"""
132
132
raise NotImplementedError (
@@ -140,7 +140,7 @@ def to_primitive(self) -> Primitive:
140
140
:const:`Primitive`: A CBOR primitive.
141
141
142
142
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
144
144
CBOR primitive types.
145
145
"""
146
146
result = self .to_shallow_primitive ()
@@ -189,6 +189,28 @@ def _dfs(value):
189
189
190
190
return _dfs (result )
191
191
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
+
192
214
@classmethod
193
215
def from_primitive (cls : CBORBase , value : Primitive ) -> CBORBase :
194
216
"""Turn a CBOR primitive to its original class type.
@@ -201,7 +223,7 @@ def from_primitive(cls: CBORBase, value: Primitive) -> CBORBase:
201
223
CBORBase: A CBOR serializable object.
202
224
203
225
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.
205
227
"""
206
228
raise NotImplementedError (
207
229
f"'from_primitive()' is not implemented by { cls .__name__ } ."
@@ -427,7 +449,7 @@ def to_shallow_primitive(self) -> List[Primitive]:
427
449
:const:`Primitive`: A CBOR primitive.
428
450
429
451
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
431
453
types.
432
454
"""
433
455
primitives = []
@@ -450,7 +472,7 @@ def from_primitive(cls: ArrayBase, values: List[Primitive]) -> ArrayBase:
450
472
:const:`ArrayBase`: Restored object.
451
473
452
474
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.
454
476
"""
455
477
all_fields = [f for f in fields (cls ) if f .init ]
456
478
if type (values ) != list :
@@ -678,7 +700,7 @@ def from_primitive(cls: DictBase, value: dict) -> DictBase:
678
700
:const:`DictBase`: Restored object.
679
701
680
702
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.
682
704
"""
683
705
if not value :
684
706
raise DeserializeException (f"Cannot accept empty value { value } ." )
0 commit comments