Zod Type Baggage
Zod also allows you to attach additional properties, referred to as “baggage”, to Zod types. This allows you to store and retrieve extra data from your Zod types. Here’s a sample usage:
In the above example, SyntheticBaggage
and SyntheticBaggageSupplier
types
are defined to represent a type of additional data that can be associated with
Zod types. This ‘baggage’ includes a string property (property1
) and a number
property (property2
). The zodBaggage
function from zod-aide
is used to
create a ‘baggage’ object (sb
). This function expects two types: one for the
baggage itself, and one for the supplier of the baggage. From this baggage
object, a ‘proxy’ is extracted which enables the ability to add baggage to any
Zod type. In this case, the proxy is applied to a Zod string type
(baggageText
). Finally, the synthetic baggage (containing the string
“baggageText” and the number 1285) is associated with baggageText
via the
proxy.
Produces: