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?
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
.