Skip to content

Commit

Permalink
fix: more reliable missing/empty file check
Browse files Browse the repository at this point in the history
  • Loading branch information
GauBen committed Oct 11, 2024
1 parent 09bcb57 commit 3cbea07
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/itchy-seas-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"formgator": patch
---

More reliable missing/empty file check
5 changes: 5 additions & 0 deletions src/validators/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe("file()", async () => {
data.append("input", f);

assert.deepEqualTyped(file().safeParse(data, "input"), succeed(f));
assert.deepEqualTyped(file().safeParse(data, "missing"), succeed(null));
assert.deepEqualTyped(
file({ multiple: true }).safeParse(data, "input"),
succeed([f]),
Expand Down Expand Up @@ -39,6 +40,10 @@ describe("file()", async () => {
data.append("ok", f);

assert.deepEqualTyped(file().safeParse(data, "input"), failures.type());
assert.deepEqualTyped(
file({ required: true }).safeParse(data, "missing"),
failures.required(),
);
assert.deepEqualTyped(
file({ multiple: true }).safeParse(data, "input"),
failures.type(),
Expand Down
5 changes: 4 additions & 1 deletion src/validators/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ export function file(
}
: (data, name) => {
const value = data.get(name);
if (!(value instanceof File)) return failures.type();
if (value !== null && !(value instanceof File))
return failures.type();
if (value === null || (value.size === 0 && value.name === ""))
return attributes.required ? failures.required() : succeed(null);
if (!accept(value)) return failures.accept(attributes.accept!);
return succeed(value);
},
Expand Down

0 comments on commit 3cbea07

Please sign in to comment.