Skip to content

Commit d5bbd1b

Browse files
kimon-satanomermecitoglu
authored andcommitted
fix: tighten isFile to reject partial schemas
1 parent 08c5126 commit d5bbd1b

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

package-lock.json

Lines changed: 13 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/zod-schema.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@ describe("isFile", () => {
1313
expect(isFile(z.string())).toBe(false);
1414
expect(isFile(z.number())).toBe(false);
1515
});
16+
17+
it("should return false for an object schema without required fields", () => {
18+
const objectSchema = z
19+
.object({
20+
name: z.string(),
21+
age: z.number(),
22+
})
23+
.partial();
24+
const result = isFile(objectSchema);
25+
expect(result).toBe(false);
26+
});
1627
});

src/utils/zod-schema.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import type { ZodType } from "zod";
22

33
export function isFile(schema: ZodType<unknown>) {
4-
const result = schema.safeParse(new File([], "nothing.txt"));
5-
return result.success;
4+
// Test that it accepts a File AND rejects a plain object
5+
const file = new File([], "nothing.txt");
6+
const plainObject = { name: "test", size: 0 };
7+
8+
const fileResult = schema.safeParse(file);
9+
const objectResult = schema.safeParse(plainObject);
10+
11+
// Should accept File but reject plain object (if it's truly a File schema)
12+
return fileResult.success && !objectResult.success;
613
}

0 commit comments

Comments
 (0)