Skip to content

Instantly share code, notes, and snippets.

@Jaysok
Last active October 24, 2022 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jaysok/00629b4ee49b884c8cfc5e0c053e81bb to your computer and use it in GitHub Desktop.
Save Jaysok/00629b4ee49b884c8cfc5e0c053e81bb to your computer and use it in GitHub Desktop.
zod transform

zod transform

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment