forked from colinhacks/zod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuple.test.ts
31 lines (27 loc) · 857 Bytes
/
tuple.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
import * as z from '../index';
import { ZodError } from '../ZodError';
import { util } from '../helpers/util';
const testTuple = z.tuple([z.string(), z.object({ name: z.literal('Rudy') }), z.array(z.literal('blue'))]);
test('tuple inference', () => {
const args1 = z.tuple([z.string()]);
const returns1 = z.number();
const func1 = z.function(args1, returns1);
type func1 = z.TypeOf<typeof func1>;
const t1: util.AssertEqual<func1, (k: string) => number> = true;
[t1];
});
test('successful validation', () => {
testTuple.parse(['asdf', { name: 'Rudy' }, ['blue']]);
});
test('failed validation', () => {
const checker = () => {
testTuple.parse([123, { name: 'Rudy2' }, ['blue', 'red']] as any);
};
try {
checker();
} catch (err) {
if (err instanceof ZodError) {
expect(err.errors.length).toEqual(3);
}
}
});