Back to TIL
June 2025
posted on 06.16.2025

Use Zod to validate File input

You can use Zod's instanceof validator to validate file inputs:

const MAX_UPLOAD_SIZE = 1024 * 1024 * 3; // 3MB
const ACCEPTED_FILE_TYPES = ['image/png'];

const Schema = z
  .instanceof(File)
  .optional()
  .refine((file) => {
    return !file || file.size <= MAX_UPLOAD_SIZE;
  }, 'File size must be less than 3MB')
  .refine((file) => {
    return ACCEPTED_FILE_TYPES.includes(file.type);
  }, 'File must be a PNG');

Above example validates the file is the correct max file size and file type.

No reactions yet

2026.10.01 14:01:34 in Naperville, IL
Last visitor from Mitaka, Japan
Theme
Color theme
⌘K