Skip to content

Commit 78b548d

Browse files
committed
Move tests
1 parent f3f2d48 commit 78b548d

File tree

2 files changed

+0
-370
lines changed

2 files changed

+0
-370
lines changed

deno/lib/__tests__/transformer.test.ts

Lines changed: 0 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -197,143 +197,7 @@ test("multiple transformers", () => {
197197
expect(doubler.parse("5")).toEqual(10);
198198
});
199199

200-
test("preprocess", () => {
201-
const schema = z.preprocess((data) => [data], z.string().array());
202200

203-
const value = schema.parse("asdf");
204-
expect(value).toEqual(["asdf"]);
205-
util.assertEqual<(typeof schema)["_input"], unknown>(true);
206-
});
207-
208-
test("async preprocess", async () => {
209-
const schema = z.preprocess(async (data) => [data], z.string().array());
210-
211-
const value = await schema.parseAsync("asdf");
212-
expect(value).toEqual(["asdf"]);
213-
});
214-
215-
test("preprocess ctx.addIssue with parse", () => {
216-
expect(() => {
217-
z.preprocess((data, ctx) => {
218-
ctx.addIssue({
219-
code: "custom",
220-
message: `${data} is not one of our allowed strings`,
221-
});
222-
return data;
223-
}, z.string()).parse("asdf");
224-
}).toThrow(
225-
JSON.stringify(
226-
[
227-
{
228-
code: "custom",
229-
message: "asdf is not one of our allowed strings",
230-
path: [],
231-
},
232-
],
233-
null,
234-
2
235-
)
236-
);
237-
});
238-
239-
test("preprocess ctx.addIssue non-fatal by default", () => {
240-
241-
try{
242-
z.preprocess((data, ctx) => {
243-
ctx.addIssue({
244-
code: "custom",
245-
message: `custom error`,
246-
});
247-
return data;
248-
}, z.string()).parse(1234);
249-
}catch(err){
250-
z.ZodError.assert(err);
251-
expect(err.issues.length).toEqual(2);
252-
}
253-
})
254-
255-
test("preprocess ctx.addIssue fatal true", () => {
256-
try{
257-
z.preprocess((data, ctx) => {
258-
ctx.addIssue({
259-
code: "custom",
260-
message: `custom error`,
261-
fatal: true
262-
});
263-
return data;
264-
}, z.string()).parse(1234);
265-
}catch(err){
266-
z.ZodError.assert(err);
267-
expect(err.issues.length).toEqual(1);
268-
}
269-
});
270-
271-
test("async preprocess ctx.addIssue with parse", async () => {
272-
const schema = z.preprocess(async (data, ctx) => {
273-
ctx.addIssue({
274-
code: "custom",
275-
message: `custom error`,
276-
});
277-
return data;
278-
}, z.string());
279-
280-
expect(schema.parseAsync("asdf")).rejects.toThrow(
281-
JSON.stringify(
282-
[
283-
{
284-
code: "custom",
285-
message: "custom error",
286-
path: [],
287-
},
288-
],
289-
null,
290-
2
291-
)
292-
);
293-
});
294-
295-
test("preprocess ctx.addIssue with parseAsync", async () => {
296-
const result = await z
297-
.preprocess(async (data, ctx) => {
298-
ctx.addIssue({
299-
code: "custom",
300-
message: `${data} is not one of our allowed strings`,
301-
});
302-
return data;
303-
}, z.string())
304-
.safeParseAsync("asdf");
305-
306-
expect(JSON.parse(JSON.stringify(result))).toEqual({
307-
success: false,
308-
error: {
309-
issues: [
310-
{
311-
code: "custom",
312-
message: "asdf is not one of our allowed strings",
313-
path: [],
314-
},
315-
],
316-
name: "ZodError",
317-
},
318-
});
319-
});
320-
321-
test("z.NEVER in preprocess", () => {
322-
const foo = z.preprocess((val, ctx) => {
323-
if (!val) {
324-
ctx.addIssue({ code: z.ZodIssueCode.custom, message: "bad" });
325-
return z.NEVER;
326-
}
327-
return val;
328-
}, z.number());
329-
330-
type foo = z.infer<typeof foo>;
331-
util.assertEqual<foo, number>(true);
332-
const arg = foo.safeParse(undefined);
333-
if (!arg.success) {
334-
expect(arg.error.issues[0].message).toEqual("bad");
335-
}
336-
});
337201

338202
test("short circuit on dirty", () => {
339203
const schema = z
@@ -353,55 +217,6 @@ test("short circuit on dirty", () => {
353217
}
354218
});
355219

356-
test("preprocess as the second property of object", () => {
357-
const schema = z.object({
358-
nonEmptyStr: z.string().min(1),
359-
positiveNum: z.preprocess((v) => Number(v), z.number().positive()),
360-
});
361-
const result = schema.safeParse({
362-
nonEmptyStr: "",
363-
positiveNum: "",
364-
});
365-
expect(result.success).toEqual(false);
366-
if (!result.success) {
367-
expect(result.error.issues.length).toEqual(2);
368-
expect(result.error.issues[0].code).toEqual(z.ZodIssueCode.too_small);
369-
expect(result.error.issues[1].code).toEqual(z.ZodIssueCode.too_small);
370-
}
371-
});
372-
373-
test("preprocess validates with sibling errors", () => {
374-
expect(() => {
375-
z.object({
376-
// Must be first
377-
missing: z.string().refine(() => false),
378-
preprocess: z.preprocess(
379-
(data: any) => data?.trim(),
380-
z.string().regex(/ asdf/)
381-
),
382-
}).parse({ preprocess: " asdf" });
383-
}).toThrow(
384-
JSON.stringify(
385-
[
386-
{
387-
code: "invalid_type",
388-
expected: "string",
389-
received: "undefined",
390-
path: ["missing"],
391-
message: "Required",
392-
},
393-
{
394-
validation: "regex",
395-
code: "invalid_string",
396-
message: "Invalid",
397-
path: ["preprocess"],
398-
},
399-
],
400-
null,
401-
2
402-
)
403-
);
404-
});
405220

406221
test("async short circuit on dirty", async () => {
407222
const schema = z

src/__tests__/transformer.test.ts

Lines changed: 0 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -196,143 +196,7 @@ test("multiple transformers", () => {
196196
expect(doubler.parse("5")).toEqual(10);
197197
});
198198

199-
test("preprocess", () => {
200-
const schema = z.preprocess((data) => [data], z.string().array());
201199

202-
const value = schema.parse("asdf");
203-
expect(value).toEqual(["asdf"]);
204-
util.assertEqual<(typeof schema)["_input"], unknown>(true);
205-
});
206-
207-
test("async preprocess", async () => {
208-
const schema = z.preprocess(async (data) => [data], z.string().array());
209-
210-
const value = await schema.parseAsync("asdf");
211-
expect(value).toEqual(["asdf"]);
212-
});
213-
214-
test("preprocess ctx.addIssue with parse", () => {
215-
expect(() => {
216-
z.preprocess((data, ctx) => {
217-
ctx.addIssue({
218-
code: "custom",
219-
message: `${data} is not one of our allowed strings`,
220-
});
221-
return data;
222-
}, z.string()).parse("asdf");
223-
}).toThrow(
224-
JSON.stringify(
225-
[
226-
{
227-
code: "custom",
228-
message: "asdf is not one of our allowed strings",
229-
path: [],
230-
},
231-
],
232-
null,
233-
2
234-
)
235-
);
236-
});
237-
238-
test("preprocess ctx.addIssue non-fatal by default", () => {
239-
240-
try{
241-
z.preprocess((data, ctx) => {
242-
ctx.addIssue({
243-
code: "custom",
244-
message: `custom error`,
245-
});
246-
return data;
247-
}, z.string()).parse(1234);
248-
}catch(err){
249-
z.ZodError.assert(err);
250-
expect(err.issues.length).toEqual(2);
251-
}
252-
})
253-
254-
test("preprocess ctx.addIssue fatal true", () => {
255-
try{
256-
z.preprocess((data, ctx) => {
257-
ctx.addIssue({
258-
code: "custom",
259-
message: `custom error`,
260-
fatal: true
261-
});
262-
return data;
263-
}, z.string()).parse(1234);
264-
}catch(err){
265-
z.ZodError.assert(err);
266-
expect(err.issues.length).toEqual(1);
267-
}
268-
});
269-
270-
test("async preprocess ctx.addIssue with parse", async () => {
271-
const schema = z.preprocess(async (data, ctx) => {
272-
ctx.addIssue({
273-
code: "custom",
274-
message: `custom error`,
275-
});
276-
return data;
277-
}, z.string());
278-
279-
expect(schema.parseAsync("asdf")).rejects.toThrow(
280-
JSON.stringify(
281-
[
282-
{
283-
code: "custom",
284-
message: "custom error",
285-
path: [],
286-
},
287-
],
288-
null,
289-
2
290-
)
291-
);
292-
});
293-
294-
test("preprocess ctx.addIssue with parseAsync", async () => {
295-
const result = await z
296-
.preprocess(async (data, ctx) => {
297-
ctx.addIssue({
298-
code: "custom",
299-
message: `${data} is not one of our allowed strings`,
300-
});
301-
return data;
302-
}, z.string())
303-
.safeParseAsync("asdf");
304-
305-
expect(JSON.parse(JSON.stringify(result))).toEqual({
306-
success: false,
307-
error: {
308-
issues: [
309-
{
310-
code: "custom",
311-
message: "asdf is not one of our allowed strings",
312-
path: [],
313-
},
314-
],
315-
name: "ZodError",
316-
},
317-
});
318-
});
319-
320-
test("z.NEVER in preprocess", () => {
321-
const foo = z.preprocess((val, ctx) => {
322-
if (!val) {
323-
ctx.addIssue({ code: z.ZodIssueCode.custom, message: "bad" });
324-
return z.NEVER;
325-
}
326-
return val;
327-
}, z.number());
328-
329-
type foo = z.infer<typeof foo>;
330-
util.assertEqual<foo, number>(true);
331-
const arg = foo.safeParse(undefined);
332-
if (!arg.success) {
333-
expect(arg.error.issues[0].message).toEqual("bad");
334-
}
335-
});
336200

337201
test("short circuit on dirty", () => {
338202
const schema = z
@@ -352,55 +216,6 @@ test("short circuit on dirty", () => {
352216
}
353217
});
354218

355-
test("preprocess as the second property of object", () => {
356-
const schema = z.object({
357-
nonEmptyStr: z.string().min(1),
358-
positiveNum: z.preprocess((v) => Number(v), z.number().positive()),
359-
});
360-
const result = schema.safeParse({
361-
nonEmptyStr: "",
362-
positiveNum: "",
363-
});
364-
expect(result.success).toEqual(false);
365-
if (!result.success) {
366-
expect(result.error.issues.length).toEqual(2);
367-
expect(result.error.issues[0].code).toEqual(z.ZodIssueCode.too_small);
368-
expect(result.error.issues[1].code).toEqual(z.ZodIssueCode.too_small);
369-
}
370-
});
371-
372-
test("preprocess validates with sibling errors", () => {
373-
expect(() => {
374-
z.object({
375-
// Must be first
376-
missing: z.string().refine(() => false),
377-
preprocess: z.preprocess(
378-
(data: any) => data?.trim(),
379-
z.string().regex(/ asdf/)
380-
),
381-
}).parse({ preprocess: " asdf" });
382-
}).toThrow(
383-
JSON.stringify(
384-
[
385-
{
386-
code: "invalid_type",
387-
expected: "string",
388-
received: "undefined",
389-
path: ["missing"],
390-
message: "Required",
391-
},
392-
{
393-
validation: "regex",
394-
code: "invalid_string",
395-
message: "Invalid",
396-
path: ["preprocess"],
397-
},
398-
],
399-
null,
400-
2
401-
)
402-
);
403-
});
404219

405220
test("async short circuit on dirty", async () => {
406221
const schema = z

0 commit comments

Comments
 (0)