Skip to content
CCPEDIAby Unity Nodes
Discussions/App Development/How to use newtype as data type in template field that returns Validation?Forum ↗

How to use newtype as data type in template field that returns Validation?

App Development2 posts169 viewsLast activity Mar 2023
DE
DemiOP
Mar 2023


How to use the smart constructor in the template so that it validates the newtype, or is there anyway to validate template fields with the data type as newtype?

ST
Stephen
Mar 2023

You can’t apply arbitrary transformations to payloads in create, because of the invariant that if create tp succeeds, fetching the resulting contract ID will respond with exactly tp. makeNewTypeSample counts as an arbitrary transformation; you could write else ok (NewtypeSample (n <> "hi")) as the last line and the function would look exactly the same from the outside.

However, you could use ensure to verify that unwrapping and reconstructing newtypeSample yields an ok with the same value.

If you do have transforming smart constructors, you could instead consider defining a separate template with a choice that is responsible for producing well-formed Template1 contracts. Choice exercise bodies do not have to follow the restrictive rules of create.

← Back to Discussions