Codecs
✨ New — Introduced in [email protected]
TLDR — You can use z.encode(<schema>, <input>)
to perform an encode operation ("reverse parsing"). This will work with any schema but is primarily intended for use in conjunction with z.codec()
.
This most important exception is the .transform()
API, which performs a unidirectional transformation. This isn't compatible with z.encode()
as Zod cannot reverse it in a sound way. If you use .transform()
anywhere in your schema, attempting a z.encode()
operation will throw a runtime error (not a ZodError
). To fix this, you'll need to refactor all usage of .transform()
to use z.codec()
.
All Zod schemas (with one major exception described below) are "codecs". That is, they can process inputs in both the forward and backward direction:
- Decoding: the "forward" direction:
Input -> Output
. This is what regular.parse()
does. - Encoding: the "backward" direction:
Output -> Input
.
You probably already know how to parse data with Zod:
Zod also provides dedicated functions for performing "decode" and "encode" operations.
In many cases (such as the string schema above), the input and output types of a Zod schema are identical, so z.decode()
and z.encode()
are functionally equivalent. But some schema types cause the input and output types to diverge:
z.default()
(input is optional, output is not)z.transform()
(a unidirectional transformation)z.codec()
(bidirectional transformation)
Most important of these is z.codec()
, which is Zod's primary mechanism for defining bidirectional transformations.
In these cases, z.decode()
and z.encode()
behave quite differently.
Note —There's nothing special about the directions or terminology here. Instead of encoding with an A -> B
codec, you could instead decode with a B -> A
codec. The use of the terms "decode" and "encode" is just a convention.
This is particularly useful when parsing data at a network boundary. You can share a single Zod schema between your client and server, then use this single schema to convert between a network-friendly format (say, JSON) and a richer JavaScript representation.
Composability
Note — You can use z.encode()
and z.decode()
with any schema. It doesn't have to be a ZodCodec.
Codecs are a schema like any other. You can nest them inside objects, arrays, pipes, etc. There are no rules on where you can use them!
Type-safe inputs
The usual .parse()
method accepts unknown
as input, and returns a value that matches the schema's inferred output type.
By constrast, the z.decode()
and z.encode()
functions have strongly-typed inputs.
Here's a diagram demonstrating the differences between the type signatures for parse()
, decode()
, and encode()
.


Async and safe variants
As with .transform()
and .refine()
, codecs support async transforms.
As with regular parse()
, there are "safe" and "async" variants of decode()
and encode()
.
How encoding works
There are some subtleties to how certain Zod schemas "reverse" their parse behavior.
Codecs
This one is fairly self-explanatory. Codecs encapsulate a bi-directional transformation between two types. During z.decode()
, the decode
transform is executed. During z.encode()
, the encode
transform is executed.
Pipes
Fun fact — Codecs are actually implemented internally as subclass of pipes that have been augmented with "interstitial" transform logic.
During regular decoding, a ZodPipe<A, B>
schema will first parse the data with A
, then pass it into B
. As you might expect, during encoding, the data is first encoded with B
, then passed into A
.
Refinements
All checks (.refine()
, .min()
, .max()
, etc.) are still executed in both directions.
To avoid unexpected errors in your custom .refine()
logic, Zod performs two "passes" during z.encode()
. The first pass ensures the input type conforms to the expected type (no invalid_type
errors). If that passes, Zod performs the second pass which executes the refinement logic.
This approach also supports "mutating transforms" like z.string().trim()
or z.string().toLowerCase()
:
Defaults and prefaults
Defaults and prefaults are only applied in the "forward" direction.
When you attach a default value to a schema, the input becomes optional (| undefined
) but the output does not. As such, undefined
is not a valid input to z.encode()
and defaults/prefaults will not be applied.
Catch
Similarly, .catch()
is only applied in the "forward" direction.
Stringbool
Note — Stringbool pre-dates the introduction of codecs in Zod. It has since been internally re-implemented as a codec.
The z.stringbool()
API converts string values ("true"
, "false"
, "yes"
, "no"
, etc.) into boolean
. By default, it will convert true
to "true"
and false
to "false"
during z.encode()
..
If you specify a custom set of truthy
and falsy
values, the first element in the array will be used instead.
Transforms
⚠️ — The .transform()
API implements a unidirectional transformation. If any .transform()
exists anywhere in your schema, attempting a z.encode()
operation will throw a runtime error (not a ZodError
).
Useful codecs
Below are implementations for a bunch of commonly-needed codecs. For the sake of customizability, these are not included as first-class APIs in Zod itself. Instead, you should copy/paste them into your project and modify them as needed.
Note — All of these codec implementations have been tested for correctness.
stringToNumber
Converts string representations of numbers to JavaScript number
type using parseFloat()
.
stringToInt
Converts string representations of integers to JavaScript number
type using parseInt()
.
stringToBigInt
Converts string representations to JavaScript bigint
type.
numberToBigInt
Converts JavaScript number
to bigint
type.
isoDatetimeToDate
Converts ISO datetime strings to JavaScript Date
objects.
epochSecondsToDate
Converts Unix timestamps (seconds since epoch) to JavaScript Date
objects.
epochMillisToDate
Converts Unix timestamps (milliseconds since epoch) to JavaScript Date
objects.
jsonCodec
Parses JSON strings into structured data and serializes back to JSON.
To further validate the resulting JSON data, pipe the result into another schema.
utf8ToBytes
Converts UTF-8 strings to Uint8Array
byte arrays.
bytesToUtf8
Converts Uint8Array
byte arrays to UTF-8 strings.
base64ToBytes
Converts base64 strings to Uint8Array
byte arrays and vice versa.
base64urlToBytes
Converts base64url strings (URL-safe base64) to Uint8Array
byte arrays.
hexToBytes
Converts hexadecimal strings to Uint8Array
byte arrays and vice versa.
stringToURL
Converts URL strings to JavaScript URL
objects.
stringToHttpURL
Converts HTTP/HTTPS URL strings to JavaScript URL
objects.
uriComponent
Encodes and decodes URI components using encodeURIComponent()
and decodeURIComponent()
.
stringToBoolean()
Converts string representations to boolean values. This is an alias for z.stringbool()
.