Skip to content

Commit 0c686af

Browse files
committed
Remove type from mime issue path
1 parent b6e59c3 commit 0c686af

File tree

3 files changed

+115
-28
lines changed

3 files changed

+115
-28
lines changed

packages/zod/src/v4/classic/tests/file.test.ts

Lines changed: 104 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,111 @@ afterEach(() => {
2020
});
2121

2222
test("passing validations", () => {
23-
minCheck.parse(new File(["12345"], "test.txt"));
24-
maxCheck.parse(new File(["12345678"], "test.txt"));
25-
mimeCheck.parse(new File([""], "test.csv", { type: "text/plain" }));
26-
expect(() => mimeCheck.parse(new File([""], "test.txt"))).toThrow();
27-
expect(() => mimeCheck.parse(new File([""], "test.txt", { type: "text/csv" }))).toThrow();
23+
expect(minCheck.safeParse(new File(["12345"], "test.txt"))).toMatchInlineSnapshot(`
24+
{
25+
"data": File {
26+
Symbol(kHandle): Blob {},
27+
Symbol(kLength): 5,
28+
Symbol(kType): "",
29+
Symbol(state): FileState {
30+
"lastModified": 1749757007218,
31+
"name": "test.txt",
32+
},
33+
},
34+
"success": true,
35+
}
36+
`);
37+
expect(maxCheck.safeParse(new File(["12345678"], "test.txt"))).toMatchInlineSnapshot(`
38+
{
39+
"data": File {
40+
Symbol(kHandle): Blob {},
41+
Symbol(kLength): 8,
42+
Symbol(kType): "",
43+
Symbol(state): FileState {
44+
"lastModified": 1749757007224,
45+
"name": "test.txt",
46+
},
47+
},
48+
"success": true,
49+
}
50+
`);
51+
expect(mimeCheck.safeParse(new File([""], "test.csv", { type: "text/plain" }))).toMatchInlineSnapshot(`
52+
{
53+
"data": File {
54+
Symbol(kHandle): Blob {},
55+
Symbol(kLength): 0,
56+
Symbol(kType): "text/plain",
57+
Symbol(state): FileState {
58+
"lastModified": 1749757007224,
59+
"name": "test.csv",
60+
},
61+
},
62+
"success": true,
63+
}
64+
`);
65+
expect(mimeCheck.safeParse(new File([""], "test.txt"))).toThrow();
66+
expect(mimeCheck.safeParse(new File([""], "test.txt", { type: "text/csv" }))).toThrow();
2867
});
2968

3069
test("failing validations", () => {
31-
expect(() => minCheck.parse(new File(["1234"], "test.txt"))).toThrow();
32-
expect(() => maxCheck.parse(new File(["123456789"], "test.txt"))).toThrow();
33-
expect(() => mimeCheck.parse(new File([""], "test.csv"))).toThrow();
34-
expect(() => mimeCheck.parse(new File([""], "test.csv", { type: "text/csv" }))).toThrow();
70+
expect(minCheck.safeParse(new File(["1234"], "test.txt"))).toMatchInlineSnapshot(`
71+
{
72+
"error": [ZodError: [
73+
{
74+
"origin": "file",
75+
"code": "too_small",
76+
"minimum": 5,
77+
"path": [],
78+
"message": "Too small: expected file to have >5 bytes"
79+
}
80+
]],
81+
"success": false,
82+
}
83+
`);
84+
expect(maxCheck.safeParse(new File(["123456789"], "test.txt"))).toMatchInlineSnapshot(`
85+
{
86+
"error": [ZodError: [
87+
{
88+
"origin": "file",
89+
"code": "too_big",
90+
"maximum": 8,
91+
"path": [],
92+
"message": "Too big: expected file to have <8 bytes"
93+
}
94+
]],
95+
"success": false,
96+
}
97+
`);
98+
expect(mimeCheck.safeParse(new File([""], "test.csv"))).toMatchInlineSnapshot(`
99+
{
100+
"error": [ZodError: [
101+
{
102+
"code": "invalid_value",
103+
"values": [
104+
"text/plain",
105+
"application/json"
106+
],
107+
"path": [],
108+
"message": "Invalid option: expected one of \\"text/plain\\"|\\"application/json\\""
109+
}
110+
]],
111+
"success": false,
112+
}
113+
`);
114+
expect(mimeCheck.safeParse(new File([""], "test.csv", { type: "text/csv" }))).toMatchInlineSnapshot(`
115+
{
116+
"error": [ZodError: [
117+
{
118+
"code": "invalid_value",
119+
"values": [
120+
"text/plain",
121+
"application/json"
122+
],
123+
"path": [],
124+
"message": "Invalid option: expected one of \\"text/plain\\"|\\"application/json\\""
125+
}
126+
]],
127+
"success": false,
128+
}
129+
`);
35130
});
36-
37-
// test("min max getters", () => {
38-
// expect(minCheck.minSize).toEqual(5);
39-
// expect(minCheck.min(10).minSize).toEqual(10);
40-
41-
// expect(maxCheck.maxSize).toEqual(8);
42-
// expect(maxCheck.max(6).maxSize).toEqual(6);
43-
// });
44-
45-
// test("accept getter", () => {
46-
// expect(mimeCheck.acceptedTypes).toEqual(["text/plain", "application/json"]);
47-
// expect(mimeCheck.type(["text/plain", "application/xml"]).acceptedTypes).toEqual(["text/plain"]);
48-
// });
49-
50-
// test("invalid mime types", () => {
51-
// expect(() => z.file().type([".txt"])).toThrow();
52-
// });

packages/zod/src/v4/core/checks.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,6 @@ export const $ZodCheckMimeType: core.$constructor<$ZodCheckMimeType> = /*@__PURE
11481148
code: "invalid_value",
11491149
values: def.mime,
11501150
input: payload.value.type,
1151-
path: ["type"],
11521151
inst,
11531152
});
11541153
};

play.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,14 @@ import { z } from "zod/v4";
22

33
z;
44

5-
console.dir(z.toJSONSchema(z.string().meta({ id: "asdf" })), { depth: null });
5+
const file = z
6+
.file()
7+
.max(5_000_000, { message: "File too large (max 5MB)" })
8+
.mime(["image/png", "image/jpeg"], { error: "Only PNG and JPEG allowed" })
9+
.optional();
10+
11+
// test parsing with invalid mime
12+
const f = new File([""], "test.txt", {
13+
type: "text/plain",
14+
});
15+
file.parse(f);

0 commit comments

Comments
 (0)