Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
make types pass WITHOUT handling schemaOptions.raw
  • Loading branch information
controversial committed Mar 6, 2025
commit 8c9d0d18fa5ad78424d2de395d533daa5c5e24fa
4 changes: 2 additions & 2 deletions zod/src/__tests__/__fixtures__/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ export const validData = {
},
],
dateStr: '2020-01-01',
} as any as z.infer<typeof schema>;
} satisfies z.input<typeof schema>;

export const invalidData = {
password: '___',
email: '',
birthYear: 'birthYear',
like: [{ id: 'z' }],
url: 'abc',
} as any as z.infer<typeof schema>;
} as unknown as z.input<typeof schema>;

export const fields: Record<InternalFieldName, Field['_f']> = {
username: {
Expand Down
10 changes: 5 additions & 5 deletions zod/src/__tests__/zod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FieldValues, Resolver, SubmitHandler, useForm } from 'react-hook-form';
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
import { z } from 'zod';
import { zodResolver } from '..';
import { fields, invalidData, schema, validData } from './__fixtures__/data';
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('zodResolver', () => {
const resolver = zodResolver(z.object({ id: z.number() }));

expectTypeOf(resolver).toEqualTypeOf<
Resolver<FieldValues, unknown, { id: number }>
Resolver<{ id: number }, unknown, { id: number }>
>();
});

Expand All @@ -109,7 +109,7 @@ describe('zodResolver', () => {
);

expectTypeOf(resolver).toEqualTypeOf<
Resolver<FieldValues, unknown, { id: string }>
Resolver<{ id: number }, unknown, { id: string }>
>();
});

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

const form = useForm({
const form = useForm<{ id: number}, unknown, { id: number }>({
resolver: zodResolver(schema),
});

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

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', () => {
const { handleSubmit } = useForm<{ id: string }, any, { id: boolean }>({
resolver: zodResolver(z.object({ id: z.boolean() })),
resolver: zodResolver(z.object({ id: z.string().transform((s) => !!s) })),
});

expectTypeOf(handleSubmit).parameter(0).toEqualTypeOf<
Expand Down
32 changes: 12 additions & 20 deletions zod/src/zod.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
import {
FieldError,
FieldErrors,
FieldValues,
Resolver,
ResolverError,
ResolverSuccess,
appendErrors,
} from 'react-hook-form';
import { ZodError, z } from 'zod';
Expand Down Expand Up @@ -84,13 +85,8 @@ export function zodResolver<
Input extends FieldValues,
Context,
Output,
Schema extends z.ZodSchema<Output, any, Input> = z.ZodSchema<
Output,
any,
Input
>,
>(
schema: Schema,
schema: z.ZodSchema<Output, any, Input>,
schemaOptions?: Partial<z.ParseParams>,
resolverOptions: {
mode?: 'async' | 'sync';
Expand All @@ -99,24 +95,20 @@ export function zodResolver<
): Resolver<
Input,
Context,
(typeof resolverOptions)['raw'] extends true
? Input
: unknown extends Output
? z.output<Schema>
: Output
Output
> {
return async (values, _, options) => {
return async (values: Input, _, options) => {
try {
const data = await schema[
resolverOptions.mode === 'sync' ? 'parse' : 'parseAsync'
](values, schemaOptions);
const data = resolverOptions.mode === 'sync'
? schema.parse(values, schemaOptions)
: await schema.parseAsync(values, schemaOptions);

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

return {
errors: {} as FieldErrors,
values: resolverOptions.raw ? Object.assign({}, values) : data,
};
errors: {},
values: data,
} satisfies ResolverSuccess<Output>;
} catch (error) {
if (isZodError(error)) {
return {
Expand All @@ -129,7 +121,7 @@ export function zodResolver<
),
options,
),
};
} satisfies ResolverError<Input>;
}

throw error;
Expand Down