Skip to content

Commit 38003a5

Browse files
authoredMay 27, 2024··
refactor: use Zod as the default validation library (#144)
TypeSchema supports multiple validation libraries, but it can cause some issues with Vercel deployments and edge runtime. This commit moves the support for multiple validation libraries to `/typeschema` path, and root path now requires Zod as validation library. re #105, #140
1 parent 0af9bbc commit 38003a5

File tree

17 files changed

+569
-514
lines changed

17 files changed

+569
-514
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<a href="https://github.com/TheEdoRan/next-safe-action"><h1>next-safe-action</h1></a>
44
</div>
55

6-
**next-safe-action** is a library that takes full advantage of the latest and greatest Next.js, React and TypeScript features, using validation libraries of your choice, to let you define **type safe** Server Actions and execute them inside React Components.
6+
**next-safe-action** is a library that takes full advantage of the latest and greatest Next.js, React and TypeScript features to let you define **type safe** Server Actions and execute them inside React Components.
77

88
## How does it work?
99

‎apps/playground/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"author": "Edoardo Ranghieri",
1212
"dependencies": {
1313
"@hookform/resolvers": "^3.3.4",
14-
"@typeschema/zod": "^0.13.3",
1514
"lucide-react": "^0.378.0",
1615
"next": "14.3.0-canary.42",
1716
"next-safe-action": "workspace:*",

‎packages/next-safe-action/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<a href="https://github.com/TheEdoRan/next-safe-action"><h1>next-safe-action</h1></a>
44
</div>
55

6-
**next-safe-action** is a library that takes full advantage of the latest and greatest Next.js, React and TypeScript features, using validation libraries of your choice, to let you define **type safe** Server Actions and execute them inside React Components.
6+
**next-safe-action** is a library that takes full advantage of the latest and greatest Next.js, React and TypeScript features to let you define **type safe** Server Actions and execute them inside React Components.
77

88
## How does it work?
99

‎packages/next-safe-action/package.json

+4-7
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
],
1212
"exports": {
1313
".": "./dist/index.mjs",
14-
"./zod": "./dist/zod.mjs",
14+
"./typeschema": "./dist/typeschema.mjs",
1515
"./hooks": "./dist/hooks.mjs"
1616
},
1717
"typesVersions": {
1818
"*": {
1919
".": [
2020
"./dist/index.d.mts"
2121
],
22-
"zod": [
23-
"./dist/zod.d.mts"
22+
"typeschema": [
23+
"./dist/typeschema.d.mts"
2424
],
2525
"hooks": [
2626
"./dist/hooks.d.mts"
@@ -80,22 +80,19 @@
8080
"peerDependencies": {
8181
"next": ">= 14.3.0-canary.42",
8282
"react": ">= 18.3.1",
83-
"@typeschema/zod": "^0.13.3",
8483
"zod": "^3.23.6"
8584
},
8685
"peerDependenciesMeta": {
8786
"zod": {
8887
"optional": true
89-
},
90-
"@typeschema/zod": {
91-
"optional": true
9288
}
9389
},
9490
"repository": {
9591
"type": "git",
9692
"url": "https://github.com/TheEdoRan/next-safe-action.git"
9793
},
9894
"dependencies": {
95+
"@typeschema/zod": "^0.13.3",
9996
"@typeschema/main": "^0.13.9"
10097
}
10198
}

‎packages/next-safe-action/src/action-builder.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { validate, type Infer, type Schema } from "@typeschema/main";
2-
import { validate as zodValidate } from "@typeschema/zod";
32
import { isNotFoundError } from "next/dist/client/components/not-found.js";
43
import { isRedirectError } from "next/dist/client/components/redirect.js";
54
import type {} from "zod";
@@ -23,6 +22,22 @@ import type {
2322
ValidationErrors,
2423
} from "./validation-errors.types";
2524

25+
async function zodValidate<S extends Schema>(s: S, data: unknown) {
26+
const result = await s.safeParseAsync(data);
27+
28+
if (result.success) {
29+
return {
30+
success: true,
31+
data: result.data as Infer<S>,
32+
} as const;
33+
}
34+
35+
return {
36+
success: false,
37+
issues: result.error.issues.map(({ message, path }) => ({ message, path })),
38+
} as const;
39+
}
40+
2641
export function actionBuilder<
2742
ServerError,
2843
S extends Schema | undefined = undefined,

‎packages/next-safe-action/src/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export type * from "./validation-errors.types";
1111

1212
/**
1313
* Create a new safe action client.
14+
* Note: this client only works with Zod as the validation library.
15+
* If you want to use a validation library supported by [TypeSchema](https://typeschema.com), import this client from `/typeschema` path.
1416
* @param createOpts Optional initialization options
1517
*
1618
* {@link https://next-safe-action.dev/docs/safe-action-client/initialization-options See docs for more information}
@@ -42,6 +44,6 @@ export const createSafeActionClient = <ServerError = string, MetadataSchema exte
4244
middlewareFns: [async ({ next }) => next({ ctx: undefined })],
4345
handleServerErrorLog,
4446
handleReturnedServerError,
45-
validationStrategy: "typeschema",
47+
validationStrategy: "zod",
4648
});
4749
};

‎packages/next-safe-action/src/zod.ts ‎packages/next-safe-action/src/typeschema.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ export type * from "./validation-errors.types";
1111

1212
/**
1313
* Create a new safe action client.
14-
* Note: this client only works with Zod as the validation library.
15-
* This is needed when TypeSchema causes problems in your application.
16-
* Check out the [troubleshooting](https://next-safe-action.dev/docs/troubleshooting/errors-with-typeschema) page of the docs for more information.
14+
* This client supports multiple validation libraries via [TypeSchema](https://typeschema.com). If you experience
15+
* issues when using this, switch to the main client exported from `next-safe-action`, which just supports Zod.
1716
* @param createOpts Optional initialization options
1817
*
1918
* {@link https://next-safe-action.dev/docs/safe-action-client/initialization-options See docs for more information}
@@ -45,6 +44,6 @@ export const createSafeActionClient = <ServerError = string, MetadataSchema exte
4544
middlewareFns: [async ({ next }) => next({ ctx: undefined })],
4645
handleServerErrorLog,
4746
handleReturnedServerError,
48-
validationStrategy: "zod",
47+
validationStrategy: "typeschema",
4948
});
5049
};

‎packages/next-safe-action/tsup.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { defineConfig } from "tsup";
22

33
export default defineConfig({
4-
entry: ["src/index.ts", "src/zod.ts", "src/hooks.ts"],
4+
entry: ["src/index.ts", "src/typeschema.ts", "src/hooks.ts"],
55
format: ["esm"],
66
clean: true,
77
splitting: false,

‎pnpm-lock.yaml

+516-441
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎website/docs/contributing.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 10
2+
sidebar_position: 9
33
description: Learn how to contribute to next-safe-action via GitHub.
44
---
55

‎website/docs/getting-started.md

+18-11
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,35 @@ description: Getting started with next-safe-action version 7.
99

1010
- Next.js >= 14.3.0-canary.40
1111
- TypeScript >= 5.0.0
12-
- a validation library supported by [TypeSchema](https://typeschema.com/#coverage)
12+
- Zod or a validation library supported by [TypeSchema](https://typeschema.com/#coverage)
1313
:::
1414

1515
**next-safe-action** provides a typesafe Server Actions implementation for Next.js App Router.
1616

17-
## Validation libraries support
18-
19-
We will use Zod as our validation library in this documentation, but since version 6 of next-safe-action, you can use your validation library of choice, or even multiple and custom ones at the same time, thanks to the **TypeSchema** library. Note that we also need to install the related TypeSchema adapter for our validation library of choice. You can find supported libraries and related adapters [here](https://typeschema.com/#coverage).
20-
21-
:::info
22-
If you experience an issue related to TypeSchema, check out the ["Errors with TypeSchema"](/docs/troubleshooting/errors-with-typeschema) section of the troubleshooting page to see how to fix it.
23-
:::
24-
2517
## Installation
2618

2719
For Next.js >= 14, assuming you want to use Zod as your validation library, use the following command:
2820

2921
```bash npm2yarn
30-
npm i next-safe-action@next zod @typeschema/zod
22+
npm i next-safe-action@next zod
23+
```
24+
25+
:::note
26+
Zod is the default validation library for next-safe-action, because TypeSchema can cause some issues with deployments, so this documentation uses it for that reason. If you know what you're doing, though, you can use your validation library of choice, or even multiple ones at the same time, thanks to the **TypeSchema** package.
27+
28+
To use this feature, you just need to update the import path for the safe client initialization function from:
29+
```typescript
30+
import { createSafeActionClient } from "next-safe-action";
31+
```
32+
33+
to:
34+
35+
```typescript
36+
import { createSafeActionClient } from "next-safe-action/typeschema";
3137
```
3238

33-
Find the adapter for your validation library of choice in the [TypeSchema documentation](https://typeschema.com/#coverage).
39+
and install the related [TypeSchema adapter](https://typeschema.com/#coverage).
40+
:::
3441

3542
## Usage
3643

‎website/docs/introduction.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
sidebar_position: 1
3-
description: next-safe-action is a library that takes full advantage of the latest and greatest Next.js, React and TypeScript features, using validation libraries of your choice, to let you define type safe Server Actions and execute them inside React Components.
3+
description: next-safe-action is a library that takes full advantage of the latest and greatest Next.js, React and TypeScript features to let you define type safe Server Actions and execute them inside React Components.
44
---
55

66
# Introduction
77

8-
**next-safe-action** is a library that takes full advantage of the latest and greatest Next.js, React and TypeScript features, using validation libraries of your choice, to let you define **type safe** Server Actions and execute them inside React Components.
8+
**next-safe-action** is a library that takes full advantage of the latest and greatest Next.js, React and TypeScript features to let you define **type safe** Server Actions and execute them inside React Components.
99

1010
## How does it work?
1111

‎website/docs/migrations/index.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 9
2+
sidebar_position: 8
33
description: Learn how to migrate from a version to the next one.
44
---
55

‎website/docs/migrations/v6-to-v7.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ Well, pretty much everything. Version 7 now works using methods; you might be fa
1414

1515
_But please still read this migration guide carefully before upgrading to v7._
1616

17-
First, install the [TypeSchema adapter](https://typeschema.com/#coverage) for the validation library of your choice to use next-safe-action v7.
18-
1917
Assuming you're using Zod, in previous versions, you'd define an auth action client and then an action like this:
2018

2119
```typescript title="action-client-v6.ts"
@@ -185,7 +183,7 @@ In version 6, you could pass metadata to actions via the third argument of the s
185183

186184
### TypeSchema update
187185

188-
TypeSchema was updated to v0.13, so now you need to install the [TypeSchema adapter](https://typeschema.com/#coverage) for the validation library of your choice to use next-safe-action.
186+
TypeSchema was updated to v0.13, so now, if you want to use a validation library other than Zod, you also need to install the related [TypeSchema adapter](https://typeschema.com/#coverage).
189187

190188
## Requirements
191189

‎website/docs/troubleshooting/errors-with-typeschema.md

-28
This file was deleted.

‎website/docs/troubleshooting/index.md

-8
This file was deleted.

‎website/src/components/landing/features.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ const features: { title: string; description: string }[] = [
2424
},
2525
{
2626
title: "Input validation using multiple validation libraries",
27-
description:
28-
"Input passed from the client to the server is validated using libraries of your choice.",
27+
description: `Input passed from the client to the server is validated using Zod or other validation libraries supported by <a href="https://typeschema.com" target="_blank" rel="noopener noreferrer">TypeSchema</a>.`,
2928
},
3029
{
3130
title: "Advanced server error handling",

0 commit comments

Comments
 (0)
Please sign in to comment.