-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidators.test.ts
112 lines (105 loc) · 3 KB
/
validators.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/ban-types */
import '@testing-library/jest-dom';
import { expectTypeOf } from 'expect-type';
import myzod from 'myzod';
import * as yup from 'yup';
import { z } from 'zod';
import * as trpc from '../src';
import { routerToServerAndClient } from './_testHelpers';
test('no validator', async () => {
const router = trpc.router().query('hello', {
resolve() {
return 'test';
},
});
const { client, close } = routerToServerAndClient(router);
const res = await client.query('hello');
expect(res).toBe('test');
close();
});
test('zod', async () => {
const router = trpc.router().query('num', {
input: z.number(),
resolve({ input }) {
return {
input,
};
},
});
const { client, close } = routerToServerAndClient(router);
const res = await client.query('num', 123);
await expect(client.query('num', '123' as any)).rejects
.toMatchInlineSnapshot(`
[TRPCClientError: [
{
"code": "invalid_type",
"expected": "number",
"received": "string",
"path": [],
"message": "Expected number, received string"
}
]]
`);
expect(res.input).toBe(123);
close();
});
test('yup', async () => {
const router = trpc.router().query('num', {
input: yup.number().required(),
resolve({ input }) {
expectTypeOf(input).toMatchTypeOf<number>();
return {
input,
};
},
});
const { client, close } = routerToServerAndClient(router);
const res = await client.query('num', 123);
await expect(client.query('num', 'asd' as any)).rejects.toMatchInlineSnapshot(
`[TRPCClientError: this must be a \`number\` type, but the final value was: \`NaN\` (cast from the value \`"asd"\`).]`,
);
expect(res.input).toBe(123);
close();
});
test('myzod', async () => {
const router = trpc.router().query('num', {
input: myzod.number(),
resolve({ input }) {
expectTypeOf(input).toMatchTypeOf<number>();
return {
input,
};
},
});
const { client, close } = routerToServerAndClient(router);
const res = await client.query('num', 123);
await expect(client.query('num', '123' as any)).rejects.toMatchInlineSnapshot(
`[TRPCClientError: expected type to be number but got string]`,
);
expect(res.input).toBe(123);
close();
});
test('validator fn', async () => {
function numParser(input: unknown) {
if (typeof input !== 'number') {
throw new Error('Not a number');
}
return input;
}
const router = trpc.router().query('num', {
input: numParser,
resolve({ input }) {
return {
input,
};
},
});
const { client, close } = routerToServerAndClient(router);
const res = await client.query('num', 123);
await expect(client.query('num', '123' as any)).rejects.toMatchInlineSnapshot(
`[TRPCClientError: Not a number]`,
);
expect(res.input).toBe(123);
close();
});