@hookform/resolvers 5.0.1 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -16
- package/package.json +2 -2
- package/standard-schema/src/__tests__/__fixtures__/data.ts +1 -1
- package/standard-schema/src/__tests__/standard-schema.ts +1 -1
- package/typebox/src/__tests__/typebox.ts +2 -2
- package/typeschema/dist/typeschema.d.ts +1 -1
- package/typeschema/dist/typeschema.js.map +1 -1
- package/typeschema/dist/typeschema.modern.mjs.map +1 -1
- package/typeschema/dist/typeschema.module.js.map +1 -1
- package/typeschema/dist/typeschema.umd.js.map +1 -1
- package/typeschema/src/__tests__/Form-native-validation.tsx +1 -1
- package/typeschema/src/__tests__/Form.tsx +1 -1
- package/typeschema/src/__tests__/__fixtures__/data.ts +1 -1
- package/typeschema/src/__tests__/typeschema.ts +1 -1
- package/typeschema/src/typeschema.ts +1 -1
- package/zod/dist/zod.d.ts +46 -7
- package/zod/dist/zod.js +1 -1
- package/zod/dist/zod.js.map +1 -1
- package/zod/dist/zod.mjs +1 -1
- package/zod/dist/zod.modern.mjs +1 -1
- package/zod/dist/zod.modern.mjs.map +1 -1
- package/zod/dist/zod.module.js +1 -1
- package/zod/dist/zod.module.js.map +1 -1
- package/zod/dist/zod.umd.js +1 -1
- package/zod/dist/zod.umd.js.map +1 -1
- package/zod/src/__tests__/Form-native-validation.tsx +1 -1
- package/zod/src/__tests__/Form.tsx +1 -1
- package/zod/src/__tests__/__fixtures__/{data.ts → data-v3.ts} +1 -1
- package/zod/src/__tests__/__fixtures__/data-v4-mini.ts +98 -0
- package/zod/src/__tests__/__fixtures__/data-v4.ts +89 -0
- package/zod/src/__tests__/zod-v3.ts +178 -0
- package/zod/src/__tests__/zod-v4-mini.ts +182 -0
- package/zod/src/__tests__/{zod.ts → zod-v4.ts} +17 -2
- package/zod/src/zod.ts +227 -53
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Field, InternalFieldName } from 'react-hook-form';
|
|
2
|
+
import { z } from 'zod/v4';
|
|
3
|
+
|
|
4
|
+
export const schema = z
|
|
5
|
+
.object({
|
|
6
|
+
username: z.string().regex(/^\w+$/).min(3).max(30),
|
|
7
|
+
password: z
|
|
8
|
+
.string()
|
|
9
|
+
.regex(new RegExp('.*[A-Z].*'), 'One uppercase character')
|
|
10
|
+
.regex(new RegExp('.*[a-z].*'), 'One lowercase character')
|
|
11
|
+
.regex(new RegExp('.*\\d.*'), 'One number')
|
|
12
|
+
.regex(
|
|
13
|
+
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'),
|
|
14
|
+
'One special character',
|
|
15
|
+
)
|
|
16
|
+
.min(8, 'Must be at least 8 characters in length'),
|
|
17
|
+
repeatPassword: z.string(),
|
|
18
|
+
accessToken: z.union([z.string(), z.number()]),
|
|
19
|
+
birthYear: z.number().min(1900).max(2013).optional(),
|
|
20
|
+
email: z.string().email().optional(),
|
|
21
|
+
tags: z.array(z.string()),
|
|
22
|
+
|
|
23
|
+
enabled: z.boolean(),
|
|
24
|
+
url: z.string().url('Custom error url').or(z.literal('')),
|
|
25
|
+
like: z
|
|
26
|
+
.array(
|
|
27
|
+
z.object({
|
|
28
|
+
id: z.number(),
|
|
29
|
+
name: z.string().length(4),
|
|
30
|
+
}),
|
|
31
|
+
)
|
|
32
|
+
.optional(),
|
|
33
|
+
dateStr: z
|
|
34
|
+
.string()
|
|
35
|
+
.transform((value) => new Date(value))
|
|
36
|
+
.refine((value) => !isNaN(value.getTime()), {
|
|
37
|
+
message: 'Invalid date',
|
|
38
|
+
}),
|
|
39
|
+
})
|
|
40
|
+
.refine((obj) => obj.password === obj.repeatPassword, {
|
|
41
|
+
message: 'Passwords do not match',
|
|
42
|
+
path: ['confirm'],
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
export const validData = {
|
|
46
|
+
username: 'Doe',
|
|
47
|
+
password: 'Password123_',
|
|
48
|
+
repeatPassword: 'Password123_',
|
|
49
|
+
birthYear: 2000,
|
|
50
|
+
email: 'john@doe.com',
|
|
51
|
+
tags: ['tag1', 'tag2'],
|
|
52
|
+
enabled: true,
|
|
53
|
+
accessToken: 'accessToken',
|
|
54
|
+
url: 'https://react-hook-form.com/',
|
|
55
|
+
like: [
|
|
56
|
+
{
|
|
57
|
+
id: 1,
|
|
58
|
+
name: 'name',
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
dateStr: '2020-01-01',
|
|
62
|
+
} satisfies z.input<typeof schema>;
|
|
63
|
+
|
|
64
|
+
export const invalidData = {
|
|
65
|
+
password: '___',
|
|
66
|
+
email: '',
|
|
67
|
+
birthYear: 'birthYear',
|
|
68
|
+
like: [{ id: 'z' }],
|
|
69
|
+
url: 'abc',
|
|
70
|
+
} as unknown as z.input<typeof schema>;
|
|
71
|
+
|
|
72
|
+
export const fields: Record<InternalFieldName, Field['_f']> = {
|
|
73
|
+
username: {
|
|
74
|
+
ref: { name: 'username' },
|
|
75
|
+
name: 'username',
|
|
76
|
+
},
|
|
77
|
+
password: {
|
|
78
|
+
ref: { name: 'password' },
|
|
79
|
+
name: 'password',
|
|
80
|
+
},
|
|
81
|
+
email: {
|
|
82
|
+
ref: { name: 'email' },
|
|
83
|
+
name: 'email',
|
|
84
|
+
},
|
|
85
|
+
birthday: {
|
|
86
|
+
ref: { name: 'birthday' },
|
|
87
|
+
name: 'birthday',
|
|
88
|
+
},
|
|
89
|
+
};
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
|
|
2
|
+
import { z } from 'zod/v3';
|
|
3
|
+
import { zodResolver } from '..';
|
|
4
|
+
import { fields, invalidData, schema, validData } from './__fixtures__/data-v3';
|
|
5
|
+
|
|
6
|
+
const shouldUseNativeValidation = false;
|
|
7
|
+
|
|
8
|
+
describe('zodResolver', () => {
|
|
9
|
+
it('should return values from zodResolver when validation pass & raw=true', async () => {
|
|
10
|
+
const parseAsyncSpy = vi.spyOn(schema, 'parseAsync');
|
|
11
|
+
|
|
12
|
+
const result = await zodResolver(schema, undefined, {
|
|
13
|
+
raw: true,
|
|
14
|
+
})(validData, undefined, {
|
|
15
|
+
fields,
|
|
16
|
+
shouldUseNativeValidation,
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
expect(parseAsyncSpy).toHaveBeenCalledTimes(1);
|
|
20
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should return parsed values from zodResolver with `mode: sync` when validation pass', async () => {
|
|
24
|
+
const parseSpy = vi.spyOn(schema, 'parse');
|
|
25
|
+
const parseAsyncSpy = vi.spyOn(schema, 'parseAsync');
|
|
26
|
+
|
|
27
|
+
const result = await zodResolver(schema, undefined, {
|
|
28
|
+
mode: 'sync',
|
|
29
|
+
})(validData, undefined, { fields, shouldUseNativeValidation });
|
|
30
|
+
|
|
31
|
+
expect(parseSpy).toHaveBeenCalledTimes(1);
|
|
32
|
+
expect(parseAsyncSpy).not.toHaveBeenCalled();
|
|
33
|
+
expect(result.errors).toEqual({});
|
|
34
|
+
expect(result).toMatchSnapshot();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should return a single error from zodResolver when validation fails', async () => {
|
|
38
|
+
const result = await zodResolver(schema)(invalidData, undefined, {
|
|
39
|
+
fields,
|
|
40
|
+
shouldUseNativeValidation,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
expect(result).toMatchSnapshot();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should return a single error from zodResolver with `mode: sync` when validation fails', async () => {
|
|
47
|
+
const parseSpy = vi.spyOn(schema, 'parse');
|
|
48
|
+
const parseAsyncSpy = vi.spyOn(schema, 'parseAsync');
|
|
49
|
+
|
|
50
|
+
const result = await zodResolver(schema, undefined, {
|
|
51
|
+
mode: 'sync',
|
|
52
|
+
})(invalidData, undefined, { fields, shouldUseNativeValidation });
|
|
53
|
+
|
|
54
|
+
expect(parseSpy).toHaveBeenCalledTimes(1);
|
|
55
|
+
expect(parseAsyncSpy).not.toHaveBeenCalled();
|
|
56
|
+
expect(result).toMatchSnapshot();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should return all the errors from zodResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
|
|
60
|
+
const result = await zodResolver(schema)(invalidData, undefined, {
|
|
61
|
+
fields,
|
|
62
|
+
criteriaMode: 'all',
|
|
63
|
+
shouldUseNativeValidation,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
expect(result).toMatchSnapshot();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('should return all the errors from zodResolver when validation fails with `validateAllFieldCriteria` set to true and `mode: sync`', async () => {
|
|
70
|
+
const result = await zodResolver(schema, undefined, { mode: 'sync' })(
|
|
71
|
+
invalidData,
|
|
72
|
+
undefined,
|
|
73
|
+
{
|
|
74
|
+
fields,
|
|
75
|
+
criteriaMode: 'all',
|
|
76
|
+
shouldUseNativeValidation,
|
|
77
|
+
},
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
expect(result).toMatchSnapshot();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should throw any error unrelated to Zod', async () => {
|
|
84
|
+
const schemaWithCustomError = schema.refine(() => {
|
|
85
|
+
throw Error('custom error');
|
|
86
|
+
});
|
|
87
|
+
const promise = zodResolver(schemaWithCustomError)(validData, undefined, {
|
|
88
|
+
fields,
|
|
89
|
+
shouldUseNativeValidation,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
await expect(promise).rejects.toThrow('custom error');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should enforce parse params type signature', async () => {
|
|
96
|
+
const resolver = zodResolver(schema, {
|
|
97
|
+
async: true,
|
|
98
|
+
path: ['asdf', 1234],
|
|
99
|
+
errorMap(iss, ctx) {
|
|
100
|
+
iss.path;
|
|
101
|
+
iss.code;
|
|
102
|
+
iss.path;
|
|
103
|
+
ctx.data;
|
|
104
|
+
ctx.defaultError;
|
|
105
|
+
return { message: 'asdf' };
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
resolver;
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Type inference tests
|
|
114
|
+
*/
|
|
115
|
+
it('should correctly infer the output type from a zod schema', () => {
|
|
116
|
+
const resolver = zodResolver(z.object({ id: z.number() }));
|
|
117
|
+
|
|
118
|
+
expectTypeOf(resolver).toEqualTypeOf<
|
|
119
|
+
Resolver<{ id: number }, unknown, { id: number }>
|
|
120
|
+
>();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('should correctly infer the output type from a zod schema using a transform', () => {
|
|
124
|
+
const resolver = zodResolver(
|
|
125
|
+
z.object({ id: z.number().transform((val) => String(val)) }),
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
expectTypeOf(resolver).toEqualTypeOf<
|
|
129
|
+
Resolver<{ id: number }, unknown, { id: string }>
|
|
130
|
+
>();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('should correctly infer the output type from a zod schema when a different input type is specified', () => {
|
|
134
|
+
const schema = z.object({ id: z.number() }).transform(({ id }) => {
|
|
135
|
+
return { id: String(id) };
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const resolver = zodResolver<{ id: number }, any, z.output<typeof schema>>(
|
|
139
|
+
schema,
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
expectTypeOf(resolver).toEqualTypeOf<
|
|
143
|
+
Resolver<{ id: number }, any, { id: string }>
|
|
144
|
+
>();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('should correctly infer the output type from a Zod schema for the handleSubmit function in useForm', () => {
|
|
148
|
+
const schema = z.object({ id: z.number() });
|
|
149
|
+
|
|
150
|
+
const form = useForm({
|
|
151
|
+
resolver: zodResolver(schema),
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
155
|
+
|
|
156
|
+
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
|
|
157
|
+
SubmitHandler<{
|
|
158
|
+
id: number;
|
|
159
|
+
}>
|
|
160
|
+
>();
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('should correctly infer the output type from a Zod schema with a transform for the handleSubmit function in useForm', () => {
|
|
164
|
+
const schema = z.object({ id: z.number().transform((val) => String(val)) });
|
|
165
|
+
|
|
166
|
+
const form = useForm({
|
|
167
|
+
resolver: zodResolver(schema),
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
171
|
+
|
|
172
|
+
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
|
|
173
|
+
SubmitHandler<{
|
|
174
|
+
id: string;
|
|
175
|
+
}>
|
|
176
|
+
>();
|
|
177
|
+
});
|
|
178
|
+
});
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
|
|
2
|
+
import { z } from 'zod/v4-mini';
|
|
3
|
+
import { zodResolver } from '..';
|
|
4
|
+
import {
|
|
5
|
+
fields,
|
|
6
|
+
invalidData,
|
|
7
|
+
schema,
|
|
8
|
+
validData,
|
|
9
|
+
} from './__fixtures__/data-v4-mini';
|
|
10
|
+
|
|
11
|
+
const shouldUseNativeValidation = false;
|
|
12
|
+
|
|
13
|
+
describe('zodResolver', () => {
|
|
14
|
+
it('should return values from zodResolver when validation pass & raw=true', async () => {
|
|
15
|
+
const parseAsyncSpy = vi.spyOn(schema, 'parseAsync');
|
|
16
|
+
|
|
17
|
+
const result = await zodResolver(schema, undefined, {
|
|
18
|
+
raw: true,
|
|
19
|
+
})(validData, undefined, {
|
|
20
|
+
fields,
|
|
21
|
+
shouldUseNativeValidation,
|
|
22
|
+
});
|
|
23
|
+
result;
|
|
24
|
+
|
|
25
|
+
expect(parseAsyncSpy).toHaveBeenCalledTimes(1);
|
|
26
|
+
expect(result).toEqual({ errors: {}, values: validData });
|
|
27
|
+
expectTypeOf(result.values);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should return parsed values from zodResolver with `mode: sync` when validation pass', async () => {
|
|
31
|
+
const parseSpy = vi.spyOn(schema, 'parse');
|
|
32
|
+
const parseAsyncSpy = vi.spyOn(schema, 'parseAsync');
|
|
33
|
+
|
|
34
|
+
const result = await zodResolver(schema, undefined, {
|
|
35
|
+
mode: 'sync',
|
|
36
|
+
})(validData, undefined, { fields, shouldUseNativeValidation });
|
|
37
|
+
expect(parseSpy).toHaveBeenCalledTimes(1);
|
|
38
|
+
expect(parseAsyncSpy).not.toHaveBeenCalled();
|
|
39
|
+
expect(result.errors).toEqual({});
|
|
40
|
+
expect(result).toMatchSnapshot();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('should return a single error from zodResolver when validation fails', async () => {
|
|
44
|
+
const result = await zodResolver(schema)(invalidData, undefined, {
|
|
45
|
+
fields,
|
|
46
|
+
shouldUseNativeValidation,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
expect(result).toMatchSnapshot();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('should return a single error from zodResolver with `mode: sync` when validation fails', async () => {
|
|
53
|
+
const parseSpy = vi.spyOn(schema, 'parse');
|
|
54
|
+
const parseAsyncSpy = vi.spyOn(schema, 'parseAsync');
|
|
55
|
+
|
|
56
|
+
const result = await zodResolver(schema, undefined, {
|
|
57
|
+
mode: 'sync',
|
|
58
|
+
})(invalidData, undefined, { fields, shouldUseNativeValidation });
|
|
59
|
+
|
|
60
|
+
expect(parseSpy).toHaveBeenCalledTimes(1);
|
|
61
|
+
expect(parseAsyncSpy).not.toHaveBeenCalled();
|
|
62
|
+
expect(result).toMatchSnapshot();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should return all the errors from zodResolver when validation fails with `validateAllFieldCriteria` set to true', async () => {
|
|
66
|
+
const result = await zodResolver(schema)(invalidData, undefined, {
|
|
67
|
+
fields,
|
|
68
|
+
criteriaMode: 'all',
|
|
69
|
+
shouldUseNativeValidation,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
expect(result).toMatchSnapshot();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should return all the errors from zodResolver when validation fails with `validateAllFieldCriteria` set to true and `mode: sync`', async () => {
|
|
76
|
+
const result = await zodResolver(schema, undefined, { mode: 'sync' })(
|
|
77
|
+
invalidData,
|
|
78
|
+
undefined,
|
|
79
|
+
{
|
|
80
|
+
fields,
|
|
81
|
+
criteriaMode: 'all',
|
|
82
|
+
shouldUseNativeValidation,
|
|
83
|
+
},
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
expect(result).toMatchSnapshot();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should throw any error unrelated to Zod', async () => {
|
|
90
|
+
const schemaWithCustomError = schema.check(
|
|
91
|
+
z.refine(() => {
|
|
92
|
+
throw Error('custom error');
|
|
93
|
+
}),
|
|
94
|
+
);
|
|
95
|
+
const promise = zodResolver(schemaWithCustomError)(validData, undefined, {
|
|
96
|
+
fields,
|
|
97
|
+
shouldUseNativeValidation,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
await expect(promise).rejects.toThrow('custom error');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Type inference tests
|
|
105
|
+
*/
|
|
106
|
+
it('should correctly infer the output type from a zod schema', () => {
|
|
107
|
+
const resolver = zodResolver(z.object({ id: z.number() }));
|
|
108
|
+
|
|
109
|
+
expectTypeOf(resolver).toEqualTypeOf<
|
|
110
|
+
Resolver<{ id: number }, unknown, { id: number }>
|
|
111
|
+
>();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should correctly infer the output type from a zod schema using a transform', () => {
|
|
115
|
+
const resolver = zodResolver(
|
|
116
|
+
z.object({
|
|
117
|
+
id: z.pipe(
|
|
118
|
+
z.number(),
|
|
119
|
+
z.transform((val) => String(val)),
|
|
120
|
+
),
|
|
121
|
+
}),
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
expectTypeOf(resolver).toEqualTypeOf<
|
|
125
|
+
Resolver<{ id: number }, unknown, { id: string }>
|
|
126
|
+
>();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('should correctly infer the output type from a zod schema when a different input type is specified', () => {
|
|
130
|
+
const schema = z.pipe(
|
|
131
|
+
z.object({ id: z.number() }),
|
|
132
|
+
z.transform(({ id }) => {
|
|
133
|
+
return { id: String(id) };
|
|
134
|
+
}),
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
const resolver = zodResolver<{ id: number }, any, z.output<typeof schema>>(
|
|
138
|
+
schema,
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
expectTypeOf(resolver).toEqualTypeOf<
|
|
142
|
+
Resolver<{ id: number }, any, { id: string }>
|
|
143
|
+
>();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('should correctly infer the output type from a Zod schema for the handleSubmit function in useForm', () => {
|
|
147
|
+
const schema = z.object({ id: z.number() });
|
|
148
|
+
|
|
149
|
+
const form = useForm({
|
|
150
|
+
resolver: zodResolver(schema),
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
154
|
+
|
|
155
|
+
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
|
|
156
|
+
SubmitHandler<{
|
|
157
|
+
id: number;
|
|
158
|
+
}>
|
|
159
|
+
>();
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('should correctly infer the output type from a Zod schema with a transform for the handleSubmit function in useForm', () => {
|
|
163
|
+
const schema = z.object({
|
|
164
|
+
id: z.pipe(
|
|
165
|
+
z.number(),
|
|
166
|
+
z.transform((val) => String(val)),
|
|
167
|
+
),
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
const form = useForm({
|
|
171
|
+
resolver: zodResolver(schema),
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
expectTypeOf(form.watch('id')).toEqualTypeOf<number>();
|
|
175
|
+
|
|
176
|
+
expectTypeOf(form.handleSubmit).parameter(0).toEqualTypeOf<
|
|
177
|
+
SubmitHandler<{
|
|
178
|
+
id: string;
|
|
179
|
+
}>
|
|
180
|
+
>();
|
|
181
|
+
});
|
|
182
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
|
|
2
|
-
import { z } from 'zod';
|
|
2
|
+
import { z } from 'zod/v4';
|
|
3
3
|
import { zodResolver } from '..';
|
|
4
|
-
import { fields, invalidData, schema, validData } from './__fixtures__/data';
|
|
4
|
+
import { fields, invalidData, schema, validData } from './__fixtures__/data-v4';
|
|
5
5
|
|
|
6
6
|
const shouldUseNativeValidation = false;
|
|
7
7
|
|
|
@@ -92,6 +92,21 @@ describe('zodResolver', () => {
|
|
|
92
92
|
await expect(promise).rejects.toThrow('custom error');
|
|
93
93
|
});
|
|
94
94
|
|
|
95
|
+
it('should enforce parse params type signature', async () => {
|
|
96
|
+
const resolver = zodResolver(schema, {
|
|
97
|
+
jitless: true,
|
|
98
|
+
reportInput: true,
|
|
99
|
+
error(iss) {
|
|
100
|
+
iss.path;
|
|
101
|
+
iss.code;
|
|
102
|
+
iss.path;
|
|
103
|
+
return { message: 'asdf' };
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
resolver;
|
|
108
|
+
});
|
|
109
|
+
|
|
95
110
|
/**
|
|
96
111
|
* Type inference tests
|
|
97
112
|
*/
|