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 flag | Type | Description |
|---|---|---|
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
| Value | Rule | Result |
|---|---|---|
"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.PDFpasses whenpdfis allowed. - Leading dots are optional in the list:
"pdf"and".pdf"are equivalent. - Works with plain strings and
Fileobjects (the file name is inspected). - Empty values pass unless
required: trueis also set.