forked from colinhacks/zod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.test.ts
More file actions
66 lines (57 loc) · 1.76 KB
/
array.test.ts
File metadata and controls
66 lines (57 loc) · 1.76 KB
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
65
66
// @ts-ignore TS6133
import { expect, test } from "@jest/globals";
import { util } from "../helpers/util";
import * as z from "../index";
const minTwo = z.string().array().min(2);
const maxTwo = z.string().array().max(2);
const justTwo = z.string().array().length(2);
const intNum = z.string().array().nonempty();
const nonEmptyMax = z.string().array().nonempty().max(2);
type t1 = z.infer<typeof nonEmptyMax>;
const f1: util.AssertEqual<[string, ...string[]], t1> = true;
f1;
type t2 = z.infer<typeof minTwo>;
const f2: util.AssertEqual<string[], t2> = true;
f2;
test("passing validations", () => {
minTwo.parse(["a", "a"]);
minTwo.parse(["a", "a", "a"]);
maxTwo.parse(["a", "a"]);
maxTwo.parse(["a"]);
justTwo.parse(["a", "a"]);
intNum.parse(["a"]);
nonEmptyMax.parse(["a"]);
});
test("failing validations", () => {
expect(() => minTwo.parse(["a"])).toThrow();
expect(() => maxTwo.parse(["a", "a", "a"])).toThrow();
expect(() => justTwo.parse(["a"])).toThrow();
expect(() => justTwo.parse(["a", "a", "a"])).toThrow();
expect(() => intNum.parse([])).toThrow();
expect(() => nonEmptyMax.parse([])).toThrow();
expect(() => nonEmptyMax.parse(["a", "a", "a"])).toThrow();
});
test("parse empty array in nonempty", () => {
expect(() =>
z
.array(z.string())
.nonempty()
.parse([] as any)
).toThrow();
});
test("get element", () => {
justTwo.element.parse("asdf");
expect(() => justTwo.element.parse(12)).toThrow();
});
test("continue parsing despite array size error", () => {
const schema = z.object({
people: z.string().array().min(2),
});
const result = schema.safeParse({
people: [123],
});
expect(result.success).toEqual(false);
if (!result.success) {
expect(result.error.issues.length).toEqual(2);
}
});