Back to TIL
June 2025
posted on 06.17.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

in Naperville, IL
Last visitor from Mitaka, Japan