If we want to input timezone
as a number
but want to parse as a string
, in this case, we can use .transform
.
const timezone = z.number().min(-11).max(14).transform((val) => {
return val < 0 ? `${val}` : `+${val}`;
});
const parsed = timezone.parse(10);
console.log(`parsed: ${parsed}, typeof: ${typeof parsed}`); // parsed: +10, typeof: string