File-instance validation
Accept browser or runtime file uploads while rejecting non-File values.
Zod · Module
z.file() creates a schema for validating File instances. Add byte-size limits with .min() and .max(), and restrict accepted content types with .mime().
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.
z.file()| Name | Type | Description |
|---|---|---|
min | number | Minimum permitted file size, in bytes. |
max | number | Maximum permitted file size, in bytes. |
mime | string | string[] | One MIME type or multiple MIME types to allow. |
A Zod file schema for validating File instances.
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..mime() to permit multiple MIME types.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.
Accept browser or runtime file uploads while rejecting non-File values.
Prevent uploads below or above permitted byte limits.
Allow only approved formats such as PNG and JPEG.
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.
.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"].
.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