Published: August 24, 2025
72
177
2.6k

Introducing Zod 4.1 and its flagship feature: CODECS z.codec() is a new API for defining *bi-directional transformations* in Zod

Image in tweet by colinhacks/zod

It is coupled with the addition of new methods (available on all Zod schemas) .decode() — "forward" parsing .encode() — "reverse" parsing

Image in tweet by colinhacks/zod

"how does .decode() differ from .parse()?" good question! unlike .parse()—which accepts any input—both encode and decode expect *strongly typed inputs*

Image in tweet by colinhacks/zod

these methods are intended for use cases involving data *conversion*, not *validation* (though they do that too). so if you pass in an input of an unexpected type, you'll get a TypeScript error.

Image in tweet by colinhacks/zod

this is useful when mapping data between two different domains. for instance, when converting data in your Node.js backend from a rich JavaScript representation (Dates, BigInts, Maps, Sets, etc) into a form that can be JSON-serialized (ISO datestrings, arrays, etc)

Image in tweet by colinhacks/zod

for demonstration, below are some worked examples. stringToBigInt:

Image in tweet by colinhacks/zod

UTF-8 string to UInt8Array

Image in tweet by colinhacks/zod

a codec for JSON parsing/stringification

Image in tweet by colinhacks/zod

instead of providing first-party APIs for commonly-needed codecs, Zod is taking a page from shadcn fully-tested versions of 16 commonly-needed codecs are available on the new Codecs page. you're encouraged to copy/paste/customize them as needed👍 https://zod.dev/codecs

including: ✅ stringToNumber ✅ stringToInt ✅ stringToBigInt ✅ numberToBigInt ✅ isoDatetimeToDate ✅ epochSecondsToDate ✅ epochMillisToDate ✅ jsonCodec ✅ utf8ToBytes ✅ bytesToUtf8 ✅ base64ToBytes ✅ base64urlToBytes ✅ hexToBytes ✅ stringToURL ✅ uriComponent

for a more detailed technical breakdown of codecs, read the associated blog post 👍 https://colinhacks.com/essays/...

@colinhacks This is awesome!

@appfactory thanks Peter!

@colinhacks How does json schema works for this? Uses input or output or ignores?

@notparbez z.toJSONSchema() defaults to output, but you can use the `io` parameter to get either input or output

Image in tweet by colinhacks/zod

@colinhacks Finally!

@MichaelArnaldi i work slow :) world's biggest hat tip to you, @GiulioCanti, and @schickling!!! 🎩🙌

@colinhacks This is huge, great way to add value to existing schemas out there!

@iwoplaza thanks! not quite as sophisticated as your codecs but it's a start 😅

@colinhacks How does infer work with it?

@RoboKozo z.infer<> is just an alias for z.output<> both z.output<> and z.input<> have existed for many years now

Image in tweet by colinhacks/zod

@colinhacks @dillon_mulroy Is there a timeline in place for the v4-mini API for this ?

@raiiii_kun @dillon_mulroy this exists in Zod Mini already (there is always 1:1 feature parity between Zod and Zod Mini) the difference is that I didn't add the .encode() and .decode() methods to Zod Mini. instead you use a top-level function, as in these examples: https://x.com/colinhacks/statu...

@colinhacks Great additions! I tried to see how z.infer would play out. Is it possible to infer either in or out?

@colinhacks @GiulioCanti looks awesome. is this gonna be part of standard schema

@moonriseTK @GiulioCanti Standard Schema, no. Standard Codec, maybe.

@colinhacks neat, excited for this!

@colinhacks lfg zod one step closer to catching up w effect Schema ;)

@colinhacks this is the main reason I started using effect schema. really incredible work here.

@colinhacks Bidirectional transformations sound powerful. What are some compelling use cases?

@colinhacks You’ve been cooking with zod recently

@colinhacks interesting api. Loved it

Share this thread

Read on Twitter

View original thread

Navigate thread

1/33