Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Zod · Module

z.file

z.file() creates a schema for validating File instances. Add byte-size limits with .min() and .max(), and restrict accepted content types with .mime().

How it works

z.file() creates a schema that accepts File instances. Chain .min() or .max() to constrain the file size in bytes, and .mime() to require one or more MIME types; parse methods then validate the input against those checks.

Syntax

z.file()

Parameters

NameTypeDescription
minnumberMinimum permitted file size, in bytes.
maxnumberMaximum permitted file size, in bytes.
mimestring | string[]One MIME type or multiple MIME types to allow.

Returns

A Zod file schema for validating File instances.

Key points

  • Use z.file() when the input must be a JavaScript File instance.
  • .min() imposes a lower bound on the file's .size, measured in bytes.
  • .max() imposes an upper bound on the file's .size, measured in bytes.
  • .mime() can require a single MIME type.
  • Pass an array to .mime() to permit multiple MIME types.

Common mistakes

  • The schema is intended for File instances; values of other types do not satisfy the base schema.

Example: Validate an image file

import { z } from "zod";

const imageSchema = z.file()
  .max(1_000_000)
  .mime(["image/png", "image/jpeg"]);

const result = imageSchema.safeParse(fileInput);
if (!result.success) {
  console.error(result.error.issues);
}

Creates a File schema with a size limit and accepted image MIME types, then validates an input without throwing on failure.

When to use it

File-instance validation

Accept browser or runtime file uploads while rejecting non-File values.

Size enforcement

Prevent uploads below or above permitted byte limits.

MIME-type filtering

Allow only approved formats such as PNG and JPEG.

Interview questions

QWhat does z.file() validate, and which constraints can be chained?

z.file() constructs a schema for validating File instances. Additional constraints can be added with .min() and .max() for byte size and .mime() for MIME type.

Follow-up: How would you restrict uploads to a bounded PNG payload? Chain .min() and .max() with .mime("image/png"), then parse the incoming value.

QHow do file-size constraints work?

.min() sets the minimum file size in bytes, while .max() sets the maximum. The documented examples use numeric byte limits such as 10_000 and 1_000_000.

Follow-up: How can several content types be accepted? Pass an array of MIME strings to .mime(), such as ["image/png", "image/jpeg"].

QHow do you allow one or multiple MIME types?

.mime() accepts a MIME string or an array of MIME strings. It lets the schema enforce the file's declared content type, such as image/png or a set of image formats.

Follow-up: Can the same schema enforce both content type and size? Yes; chain .mime() together with .min() or .max() before parsing.

  • z.file().min()
  • z.file().max()
  • z.file().mime()
  • File

Source: Defining schemas - Zod. Checked 2026-10-15.