Skip to content

feat: support Zod 4 #777

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

Merged
merged 5 commits into from
Jun 7, 2025
Merged
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Example:
```tsx
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
import { z } from 'zod'; // or 'zod/v4'

const schema = z.object({
id: z.number(),
Expand Down Expand Up @@ -175,7 +175,7 @@ const App = () => {
};
```

### [Zod](https://github.com/vriad/zod)
### [Zod](https://github.com/colinhacks/zod)

TypeScript-first schema validation with static type inference

Expand All @@ -186,7 +186,7 @@ TypeScript-first schema validation with static type inference
```tsx
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
import { z } from 'zod'; // or 'zod/v4'

const schema = z.object({
name: z.string().min(1, { message: 'Required' }),
Expand Down
4 changes: 2 additions & 2 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"vite-tsconfig-paths": "^5.1.4",
"vitest": "^3.0.9",
"yup": "^1.6.1",
"zod": "^3.24.2",
"zod": "^3.25.0",
},
"peerDependencies": {
"react-hook-form": "^7.55.0",
Expand Down Expand Up @@ -1444,7 +1444,7 @@

"yup": ["yup@1.6.1", "", { "dependencies": { "property-expr": "^2.0.5", "tiny-case": "^1.0.3", "toposort": "^2.0.2", "type-fest": "^2.19.0" } }, "sha512-JED8pB50qbA4FOkDol0bYF/p60qSEDQqBD0/qeIrUCG1KbPBIQ776fCUNb9ldbPcSTxA69g/47XTo4TqWiuXOA=="],

"zod": ["zod@3.24.2", "", {}, "sha512-lY7CDW43ECgW9u1TcT3IoXHflywfVqDYze4waEz812jR/bZ8FHDsl7pFQoSZTz5N+2NqRXs8GBwnAwo3ZNxqhQ=="],
"zod": ["zod@3.25.51", "", {}, "sha512-TQSnBldh+XSGL+opiSIq0575wvDPqu09AqWe1F7JhUMKY+M91/aGlK4MhpVNO7MgYfHcVCB1ffwAUTJzllKJqg=="],

"@asamuzakjp/css-color/lru-cache": ["lru-cache@10.4.3", "", {}, "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ=="],

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
"vite-tsconfig-paths": "^5.1.4",
"vitest": "^3.0.9",
"yup": "^1.6.1",
"zod": "^3.24.2"
"zod": "^3.25.0"
},
"peerDependencies": {
"react-hook-form": "^7.55.0"
Expand Down
2 changes: 1 addition & 1 deletion standard-schema/src/__tests__/__fixtures__/data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StandardSchemaV1 } from '@standard-schema/spec';
import { Field, InternalFieldName } from 'react-hook-form';
import { z } from 'zod';
import { z } from 'zod/v3';

export const schema = z
.object({
Expand Down
2 changes: 1 addition & 1 deletion standard-schema/src/__tests__/standard-schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
import { z } from 'zod';
import { z } from 'zod/v3';
import { standardSchemaResolver } from '..';
import {
customSchema,
Expand Down
4 changes: 2 additions & 2 deletions typebox/src/__tests__/typebox.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Type } from '@sinclair/typebox';
import { TypeCompiler } from '@sinclair/typebox/compiler';
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
import { typeboxResolver } from '..';
import { fields, invalidData, schema, validData } from './__fixtures__/data';
import { Type } from '@sinclair/typebox';
import { TypeCompiler } from '@sinclair/typebox/compiler';

const shouldUseNativeValidation = false;

Expand Down
2 changes: 1 addition & 1 deletion typeschema/src/__tests__/Form-native-validation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import user from '@testing-library/user-event';
import type { Infer } from '@typeschema/main';
import React from 'react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { z } from 'zod/v3';
import { typeschemaResolver } from '..';

const USERNAME_REQUIRED_MESSAGE = 'username field is required';
Expand Down
2 changes: 1 addition & 1 deletion typeschema/src/__tests__/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import user from '@testing-library/user-event';
import type { Infer } from '@typeschema/main';
import React from 'react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { z } from 'zod/v3';
import { typeschemaResolver } from '..';

const schema = z.object({
Expand Down
2 changes: 1 addition & 1 deletion typeschema/src/__tests__/__fixtures__/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Field, InternalFieldName } from 'react-hook-form';
import { z } from 'zod';
import { z } from 'zod/v3';

export const schema = z
.object({
Expand Down
2 changes: 1 addition & 1 deletion typeschema/src/__tests__/typeschema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as typeschema from '@typeschema/main';
import { Resolver, SubmitHandler, useForm } from 'react-hook-form';
import { z } from 'zod';
import { z } from 'zod/v3';
import { typeschemaResolver } from '..';
import { fields, invalidData, schema, validData } from './__fixtures__/data';

Expand Down
2 changes: 1 addition & 1 deletion typeschema/src/typeschema.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { toNestErrors, validateFieldsNatively } from '@hookform/resolvers';
import { StandardSchemaV1 } from '@standard-schema/spec';
import {
FieldError,
FieldErrors,
FieldValues,
Resolver,
appendErrors,
} from 'react-hook-form';
import { StandardSchemaV1 } from 'zod/lib/standard-schema';

const parseErrorSchema = (
typeschemaErrors: readonly StandardSchemaV1.Issue[],
Expand Down
2 changes: 1 addition & 1 deletion zod/src/__tests__/Form-native-validation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { render, screen } from '@testing-library/react';
import user from '@testing-library/user-event';
import React from 'react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { z } from 'zod/v3';
import { zodResolver } from '..';

const USERNAME_REQUIRED_MESSAGE = 'username field is required';
Expand Down
2 changes: 1 addition & 1 deletion zod/src/__tests__/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { render, screen } from '@testing-library/react';
import user from '@testing-library/user-event';
import React from 'react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { z } from 'zod/v3';
import { zodResolver } from '..';

const schema = z.object({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Field, InternalFieldName } from 'react-hook-form';
import { z } from 'zod';
import { z } from 'zod/v3';

export const schema = z
.object({
Expand Down
98 changes: 98 additions & 0 deletions zod/src/__tests__/__fixtures__/data-v4-mini.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { Field, InternalFieldName } from 'react-hook-form';
import { z } from 'zod/v4-mini';

export const schema = z
.object({
username: z
.string()
.check(z.regex(/^\w+$/), z.minLength(3), z.maxLength(30)),
password: z
.string()
.check(
z.regex(new RegExp('.*[A-Z].*'), 'One uppercase character'),
z.regex(new RegExp('.*[a-z].*'), 'One lowercase character'),
z.regex(new RegExp('.*\\d.*'), 'One number'),
z.regex(
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'),
'One special character',
),
z.minLength(8, 'Must be at least 8 characters in length'),
),
repeatPassword: z.string(),
accessToken: z.union([z.string(), z.number()]),
birthYear: z.optional(z.number().check(z.minimum(1900), z.maximum(2013))),
email: z.optional(z.email()),
tags: z.array(z.string()),
enabled: z.boolean(),
url: z.union([z.url('Custom error url'), z.literal('')]),
like: z.optional(
z.array(
z.object({
id: z.number(),
name: z.string().check(z.length(4)),
}),
),
),
dateStr: z
.pipe(
z.string(),
z.transform((value) => new Date(value)),
)
.check(
z.refine((value) => !isNaN(value.getTime()), {
message: 'Invalid date',
}),
),
})
.check(
z.refine((obj) => obj.password === obj.repeatPassword, {
message: 'Passwords do not match',
path: ['confirm'],
}),
);

export const validData = {
username: 'Doe',
password: 'Password123_',
repeatPassword: 'Password123_',
birthYear: 2000,
email: 'john@doe.com',
tags: ['tag1', 'tag2'],
enabled: true,
accessToken: 'accessToken',
url: 'https://react-hook-form.com/',
like: [
{
id: 1,
name: 'name',
},
],
dateStr: '2020-01-01',
} satisfies z.input<typeof schema>;

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

export const fields: Record<InternalFieldName, Field['_f']> = {
username: {
ref: { name: 'username' },
name: 'username',
},
password: {
ref: { name: 'password' },
name: 'password',
},
email: {
ref: { name: 'email' },
name: 'email',
},
birthday: {
ref: { name: 'birthday' },
name: 'birthday',
},
};
89 changes: 89 additions & 0 deletions zod/src/__tests__/__fixtures__/data-v4.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Field, InternalFieldName } from 'react-hook-form';
import { z } from 'zod/v4';

export const schema = z
.object({
username: z.string().regex(/^\w+$/).min(3).max(30),
password: z
.string()
.regex(new RegExp('.*[A-Z].*'), 'One uppercase character')
.regex(new RegExp('.*[a-z].*'), 'One lowercase character')
.regex(new RegExp('.*\\d.*'), 'One number')
.regex(
new RegExp('.*[`~<>?,./!@#$%^&*()\\-_+="\'|{}\\[\\];:\\\\].*'),
'One special character',
)
.min(8, 'Must be at least 8 characters in length'),
repeatPassword: z.string(),
accessToken: z.union([z.string(), z.number()]),
birthYear: z.number().min(1900).max(2013).optional(),
email: z.string().email().optional(),
tags: z.array(z.string()),

enabled: z.boolean(),
url: z.string().url('Custom error url').or(z.literal('')),
like: z
.array(
z.object({
id: z.number(),
name: z.string().length(4),
}),
)
.optional(),
dateStr: z
.string()
.transform((value) => new Date(value))
.refine((value) => !isNaN(value.getTime()), {
message: 'Invalid date',
}),
})
.refine((obj) => obj.password === obj.repeatPassword, {
message: 'Passwords do not match',
path: ['confirm'],
});

export const validData = {
username: 'Doe',
password: 'Password123_',
repeatPassword: 'Password123_',
birthYear: 2000,
email: 'john@doe.com',
tags: ['tag1', 'tag2'],
enabled: true,
accessToken: 'accessToken',
url: 'https://react-hook-form.com/',
like: [
{
id: 1,
name: 'name',
},
],
dateStr: '2020-01-01',
} satisfies z.input<typeof schema>;

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

export const fields: Record<InternalFieldName, Field['_f']> = {
username: {
ref: { name: 'username' },
name: 'username',
},
password: {
ref: { name: 'password' },
name: 'password',
},
email: {
ref: { name: 'email' },
name: 'email',
},
birthday: {
ref: { name: 'birthday' },
name: 'birthday',
},
};
Loading