/
unions.test.ts
64 lines (57 loc) · 1.57 KB
/
unions.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts";
const test = Deno.test;
import * as z from "../index.ts";
test("function parsing", () => {
const schema = z.union([
z.string().refine(() => false),
z.number().refine(() => false),
]);
const result = schema.safeParse("asdf");
expect(result.success).toEqual(false);
});
test("union 2", () => {
const result = z
.union([z.number(), z.string().refine(() => false)])
.safeParse("a");
expect(result.success).toEqual(false);
});
test("return valid over invalid", () => {
const schema = z.union([
z.object({
email: z.string().email(),
}),
z.string(),
]);
expect(schema.parse("asdf")).toEqual("asdf");
expect(schema.parse({ email: "asdlkjf@lkajsdf.com" })).toEqual({
email: "asdlkjf@lkajsdf.com",
});
});
test("return dirty result over aborted", () => {
const result = z
.union([z.number(), z.string().refine(() => false)])
.safeParse("a");
expect(result.success).toEqual(false);
if (!result.success) {
expect(result.error.issues).toEqual([
{
code: "custom",
message: "Invalid input",
path: [],
},
]);
}
});
test("options getter", async () => {
const union = z.union([z.string(), z.number()]);
union.options[0].parse("asdf");
union.options[1].parse(1234);
await union.options[0].parseAsync("asdf");
await union.options[1].parseAsync(1234);
});
test("readonly union", async () => {
const options = [z.string(), z.number()] as const;
const union = z.union(options);
union.parse("asdf");
union.parse(12);
});