Skip to content

fix(zod): update return type in zodResolver to use z.input instead of… #749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 9 additions & 4 deletions zod/src/__tests__/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ const schema = z.object({
password: z.string().nonempty({ message: 'password field is required' }),
});

type FormData = z.infer<typeof schema> & { unusedProperty: string };
type SchemaInput = z.input<typeof schema>;
type FormData = z.output<typeof schema> & { unusedProperty: string };

function TestComponent({
onSubmit,
}: { onSubmit: (data: z.infer<typeof schema>) => void }) {
}: { onSubmit: (data: z.output<typeof schema>) => void }) {
const {
register,
handleSubmit,
formState: { errors },
} = useForm({
} = useForm<SchemaInput, unknown, FormData>({
resolver: zodResolver(schema), // Useful to check TypeScript regressions
defaultValues: {
username: '',
password: '',
},
});

return (
Expand Down Expand Up @@ -58,7 +63,7 @@ export function TestComponentManualType({
register,
handleSubmit,
formState: { errors },
} = useForm<z.infer<typeof schema>, undefined, FormData>({
} = useForm<SchemaInput, undefined, FormData>({
resolver: zodResolver(schema), // Useful to check TypeScript regressions
});

Expand Down
4 changes: 2 additions & 2 deletions zod/src/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function parseErrorSchema(
* @param {Object} [resolverOptions] - Optional resolver-specific configuration
* @param {('async'|'sync')} [resolverOptions.mode='async'] - Validation mode. Use 'sync' for synchronous validation
* @param {boolean} [resolverOptions.raw=false] - If true, returns the raw form values instead of the parsed data
* @returns {Resolver<z.infer<typeof schema>>} A resolver function compatible with react-hook-form
* @returns {Resolver<z.input<typeof schema>>} A resolver function compatible with react-hook-form
* @throws {Error} Throws if validation fails with a non-Zod error
* @example
* const schema = z.object({
Expand All @@ -87,7 +87,7 @@ export function zodResolver<TFieldValues extends FieldValues>(
mode?: 'async' | 'sync';
raw?: boolean;
} = {},
): Resolver<z.infer<typeof schema>> {
): Resolver<z.input<typeof schema>> {
return async (values, _, options) => {
try {
const data = await schema[
Expand Down