Formify Docs

File Extension

Restrict the allowed extensions of uploaded files

File Extension

The fileExtension rule validates that a value — a file name, path, or File object — ends with one of a list of allowed extensions. Ideal for file-upload fields.

Configuration

Config flagTypeDescription
fileExtension{ extensions: string[] }Allowed extensions, with or without the leading dot.
validationSchema: {
  resume: { fileExtension: { extensions: ["pdf", "docx"] } },
}

With a custom message:

resume: { fileExtension: { extensions: ["pdf", "docx"], message: "Only PDF or DOCX files are allowed" } }

V builder

import { V } from "formify-js";

V.fileExtension(["pdf", "docx"])
V.fileExtension(["pdf", "docx"], "Only PDF or DOCX files are allowed")

Default message

The default message names the allowed extensions, e.g. File must be one of: pdf, docx.

Examples

ValueRuleResult
"resume.pdf"{ extensions: ["pdf", "docx"] }✅ Valid
"resume.docx"{ extensions: ["pdf", "docx"] }✅ Valid
"resume.txt"{ extensions: ["pdf", "docx"] }❌ Invalid

Things to know

  • The check is case-insensitive: resume.PDF passes when pdf is allowed.
  • Leading dots are optional in the list: "pdf" and ".pdf" are equivalent.
  • Works with plain strings and File objects (the file name is inspected).
  • Empty values pass unless required: true is also set.

On this page