io-ts has a runtime type system for IO decoding/encoding, which allows defining an input and output type and a function to convert between these two types. This functionality enables us to not only validate an object but also convert parts of the input object to a different type.
Here is an example of Duration defined in Kibana. It receives a string like 1m and if it is valid, it returns a Duration object.
io-ts also allows us to get the whole input and output type at the top level, so type conversion can happen anywhere in the nested objects (example).
const createSLOParamsSchema = t.type({
body: t.intersection([
t.type({
...
timeWindow: **timeWindowSchema**,
...
}),
...
]),
});
const timeWindowSchema = t.union([**rollingTimeWindowSchema**, calendarAlignedTimeWindowSchema]);
const rollingTimeWindowSchema = t.type({
duration: **durationType**, <- This is the Duration type
type: rollingTimeWindowTypeSchema,
});
type CreateSLOInput = t.OutputOf<typeof createSLOParamsSchema.props.body>;
type CreateSLOParams = t.TypeOf<typeof createSLOParamsSchema.props.body>;
Zod seems to have a similar functionality called transform.
🍒 Summary
io-ts has a runtime type system for IO decoding/encoding, which allows defining an input and output type and a function to convert between these two types. This functionality enables us to not only validate an object but also convert parts of the input object to a different type.
Here is an example of Duration defined in Kibana. It receives a string like
1m
and if it is valid, it returns a Duration object.io-ts also allows us to get the whole input and output type at the top level, so type conversion can happen anywhere in the nested objects (example).
Zod seems to have a similar functionality called transform.