Zod logo

@zod/mini

The @zod/mini library was introduced with the release of Zod 4.

npm install @zod/mini@next

It implements the exact same functionality as zod, but using a functional, tree-shakable API. If you're coming from zod, this means you generally will use functions in place of methods.

import * as z from "@zod/mini";
 
const mySchema = z.nullable(z.optional(z.string()));

Tree-shaking — In zod, schemas provide a range of convenience methods to perform some common operations (e.g. .min() on string schemas). Bundlers have a hard time removing ("tree shaking") method implementations from your bundle, even if they aren't used. It's much easier to remove an unused top-level function, which is why the API of @zod/mini uses more functions than methods.

ZodMiniType

All @zod/mini schemas extend the z.ZodMiniType base class, which in turn extends z.core.$ZodType from @zod/core. While this class implements far fewer methods than ZodType in zod, some particularly useful methods remain.

.parse

This is an obvious one. All @zod/mini schemas implement the same parsing methods as zod.

import * as z from "@zod/mini";
 
const mySchema = z.string();
 
mySchema.parse('asdf')
await mySchema.parseAsync('asdf')
mySchema.safeParse('asdf')
await mySchema.safeParseAsync('asdf')

.check()

In zod there are dedicated methods on schema subclasses for performing common checks:

import * as z from "zod";
 
z.string()
  .min(5)
  .max(10)
  .refine(val => val.includes("@"))
  .trim()

In @zod/mini such methods aren't implemented. Instead you pass these checks into schemas using the .check() method:

import * as z from "@zod/mini";
 
z.string().check(
  z.minLength(5), 
  z.maxLength(10),
  z.refine(val => val.includes("@")),
  z.trim()
);

The following checks are implemented. Some of these checks only apply to schemas of certain types (e.g. strings or numbers). The APIs are all type-safe; TypeScript won't let you add an unsupported check to your schema.

z.lt(value);
z.lte(value); // alias: z.maximum()
z.gt(value);
z.gte(value); // alias: z.minimum()
z.positive();
z.negative();
z.nonpositive();
z.nonnegative();
z.multipleOf(value);
z.maxSize(value);
z.minSize(value);
z.size(value);
z.maxLength(value);
z.minLength(value);
z.length(value);
z.regex(regex);
z.lowercase();
z.uppercase();
z.includes(value);
z.startsWith(value);
z.endsWith(value);
z.property(key, schema);
z.mime(value);
 
// custom checks
z.refine()
z.check()   // replaces .superRefine()
 
// mutations (these do not change the inferred types)
z.overwrite(value => newValue);
z.normalize();
z.trim();
z.toLowerCase();
z.toUpperCase();

.register()

For registering a schema in a registry.

const myReg = z.registry<{title: string}>();
 
z.string().register(myReg, { title: "My cool string schema" });

.brand()

For branding a schema. Refer to the Branded types docs for more information.

import * as z from "@zod/mini";
 
const USD = z.string().brand("USD");

.clone(def)

Returns an identical clone of the current schema using the provided def.

const mySchema = z.string()
 
mySchema.clone(mySchema._zod.def);

No default locale

While zod automatically loads the English (en) locale, @zod/mini does not. This reduces the bundle size in scenarios where error messages are unnecessary, localized to a non-English language, or otherwise customized.

This means, by default the message property of all issues will simply read "Invalid input". To load the English locale:

import * as z from "@zod/mini";
 
z.config(z.core.locales.en());

Refer to the Locales docs for more on localization.

On this page