Skip to content

Commit 8c9d0d1

Browse files
committed
make types pass WITHOUT handling schemaOptions.raw
1 parent d3aa26b commit 8c9d0d1

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

zod/src/__tests__/__fixtures__/data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ export const validData = {
5858
},
5959
],
6060
dateStr: '2020-01-01',
61-
} as any as z.infer<typeof schema>;
61+
} satisfies z.input<typeof schema>;
6262

6363
export const invalidData = {
6464
password: '___',
6565
email: '',
6666
birthYear: 'birthYear',
6767
like: [{ id: 'z' }],
6868
url: 'abc',
69-
} as any as z.infer<typeof schema>;
69+
} as unknown as z.input<typeof schema>;
7070

7171
export const fields: Record<InternalFieldName, Field['_f']> = {
7272
username: {

zod/src/__tests__/zod.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FieldValues, Resolver, SubmitHandler, useForm } from 'react-hook-form';
1+
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
22
import { z } from 'zod';
33
import { zodResolver } from '..';
44
import { fields, invalidData, schema, validData } from './__fixtures__/data';
@@ -99,7 +99,7 @@ describe('zodResolver', () => {
9999
const resolver = zodResolver(z.object({ id: z.number() }));
100100

101101
expectTypeOf(resolver).toEqualTypeOf<
102-
Resolver<FieldValues, unknown, { id: number }>
102+
Resolver<{ id: number }, unknown, { id: number }>
103103
>();
104104
});
105105

@@ -109,7 +109,7 @@ describe('zodResolver', () => {
109109
);
110110

111111
expectTypeOf(resolver).toEqualTypeOf<
112-
Resolver<FieldValues, unknown, { id: string }>
112+
Resolver<{ id: number }, unknown, { id: string }>
113113
>();
114114
});
115115

@@ -130,7 +130,7 @@ describe('zodResolver', () => {
130130
it('should correctly infer the output type from a Zod schema for the handleSubmit function in useForm', () => {
131131
const schema = z.object({ id: z.number() });
132132

133-
const form = useForm({
133+
const form = useForm<{ id: number}, unknown, { id: number }>({
134134
resolver: zodResolver(schema),
135135
});
136136

@@ -145,7 +145,7 @@ describe('zodResolver', () => {
145145

146146
it('should correctly infer the output type from a Zod schema when different input and output types are specified for the handleSubmit function in useForm', () => {
147147
const { handleSubmit } = useForm<{ id: string }, any, { id: boolean }>({
148-
resolver: zodResolver(z.object({ id: z.boolean() })),
148+
resolver: zodResolver(z.object({ id: z.string().transform((s) => !!s) })),
149149
});
150150

151151
expectTypeOf(handleSubmit).parameter(0).toEqualTypeOf<

zod/src/zod.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
22
import {
33
FieldError,
4-
FieldErrors,
54
FieldValues,
65
Resolver,
6+
ResolverError,
7+
ResolverSuccess,
78
appendErrors,
89
} from 'react-hook-form';
910
import { ZodError, z } from 'zod';
@@ -84,13 +85,8 @@ export function zodResolver<
8485
Input extends FieldValues,
8586
Context,
8687
Output,
87-
Schema extends z.ZodSchema<Output, any, Input> = z.ZodSchema<
88-
Output,
89-
any,
90-
Input
91-
>,
9288
>(
93-
schema: Schema,
89+
schema: z.ZodSchema<Output, any, Input>,
9490
schemaOptions?: Partial<z.ParseParams>,
9591
resolverOptions: {
9692
mode?: 'async' | 'sync';
@@ -99,24 +95,20 @@ export function zodResolver<
9995
): Resolver<
10096
Input,
10197
Context,
102-
(typeof resolverOptions)['raw'] extends true
103-
? Input
104-
: unknown extends Output
105-
? z.output<Schema>
106-
: Output
98+
Output
10799
> {
108-
return async (values, _, options) => {
100+
return async (values: Input, _, options) => {
109101
try {
110-
const data = await schema[
111-
resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'
112-
](values, schemaOptions);
102+
const data = resolverOptions.mode === 'sync'
103+
? schema.parse(values, schemaOptions)
104+
: await schema.parseAsync(values, schemaOptions);
113105

114106
options.shouldUseNativeValidation && validateFieldsNatively({}, options);
115107

116108
return {
117-
errors: {} as FieldErrors,
118-
values: resolverOptions.raw ? Object.assign({}, values) : data,
119-
};
109+
errors: {},
110+
values: data,
111+
} satisfies ResolverSuccess<Output>;
120112
} catch (error) {
121113
if (isZodError(error)) {
122114
return {
@@ -129,7 +121,7 @@ export function zodResolver<
129121
),
130122
options,
131123
),
132-
};
124+
} satisfies ResolverError<Input>;
133125
}
134126

135127
throw error;

0 commit comments

Comments
 (0)