Skip to content

Commit c3aba3b

Browse files
committed
chore(website): add validation libraries support recipe
1 parent fcfb25a commit c3aba3b

13 files changed

+64
-46
lines changed

website/docs/getting-started.md

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ description: Getting started with next-safe-action version 7.
1010
- Next.js >= 14 (>= 15 for [`useStateAction`](/docs/execution/hooks/usestateaction) hook)
1111
- React >= 18.2.0
1212
- TypeScript >= 5
13-
- Zod or a validation library supported by [TypeSchema](https://typeschema.com/#coverage)
13+
- Zod or Valibot or Yup
1414
:::
1515

1616
**next-safe-action** provides a typesafe Server Actions implementation for Next.js App Router.
@@ -24,20 +24,7 @@ npm i next-safe-action zod
2424
```
2525

2626
:::note
27-
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.
28-
29-
To use this feature, you just need to update the import path for the safe client initialization function from:
30-
```typescript
31-
import { createSafeActionClient } from "next-safe-action";
32-
```
33-
34-
to:
35-
36-
```typescript
37-
import { createSafeActionClient } from "next-safe-action/typeschema";
38-
```
39-
40-
and install the related [TypeSchema adapter](https://typeschema.com/#coverage).
27+
Zod is the default validation library for next-safe-action. If you want to use a different validation library, check out the [validation libraries support](/docs/recipes/validation-libraries-support) recipe.
4128
:::
4229

4330
## Usage

website/docs/recipes/additional-validation-errors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 4
2+
sidebar_position: 5
33
description: Set additional custom validation errors in schema or in action's server code function.
44
---
55

website/docs/recipes/customize-validation-errors-format.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 5
2+
sidebar_position: 6
33
description: Learn how to customize validation errors format returned to the client.
44
---
55

website/docs/recipes/extend-base-client.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 2
2+
sidebar_position: 3
33
description: Learn how to use both a basic and an authorization client at the same time in your project.
44
---
55

website/docs/recipes/extend-previous-schema.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 3
2+
sidebar_position: 4
33
description: Learn how to use next-safe-action with a i18n solution.
44
---
55

website/docs/recipes/form-actions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 6
2+
sidebar_position: 7
33
description: Learn how to use next-safe-action with Form Actions.
44
---
55

website/docs/recipes/i18n.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 8
2+
sidebar_position: 9
33
description: Learn how to use next-safe-action with a i18n solution.
44
---
55

website/docs/recipes/upload-files.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 7
2+
sidebar_position: 8
33
description: Learn how to upload a file using next-safe-action.
44
---
55

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
sidebar_position: 2
3+
description: Use a validation library of your choice with next-safe-action.
4+
---
5+
6+
# Validation libraries support
7+
8+
Starting from version 6.0.0, and up to version 7.1.3, next-safe-action used [TypeSchema](https://typeschema.com/) to enable support for multiple validation libraries. This has worked pretty well, but caused some issues too, such as the [Edge Runtime incompatibility](/docs/troubleshooting#typeschema-issues-with-edge-runtime) or [lack of support for TypeScript >= 5.5](/docs/troubleshooting#schema-and-parsedinput-are-typed-any-broken-types-and-build-issues).
9+
10+
To solve these issues, next-safe-action v7.2.0 and later versions ship with a built-in modular support for multiple validation libraries, at this time: Zod, Valibot and Yup.
11+
12+
If you used a TypeSchema adapter before, you should uninstall it, since you just need the validation library of your choice from now on.
13+
14+
The configuration is pretty simple. If you use Zod, you don't have to do anything. If you choose to use Valibot or Yup, other than obviously installing the validation library itself, you need to specify the correct validation adapter when you're initializing the safe action client:
15+
16+
17+
For Valibot:
18+
19+
```typescript title="@/lib/safe-action.ts"
20+
import { createSafeActionClient } from "next-safe-action";
21+
import { valibotAdapter } from "next-safe-action/adapters/valibot"; // import the adapter
22+
23+
export const actionClient = createSafeActionClient({
24+
validationAdapter: valibotAdapter(), // <-- and then pass it to the client
25+
// other options here...
26+
});
27+
```
28+
29+
For Yup:
30+
31+
```typescript title="@/lib/safe-action.ts"
32+
import { createSafeActionClient } from "next-safe-action";
33+
import { yupAdapter } from "next-safe-action/adapters/yup"; // import the adapter
34+
35+
export const actionClient = createSafeActionClient({
36+
validationAdapter: yupAdapter(), // <-- and then pass it to the client
37+
// other options here...
38+
});
39+
```
40+
41+
And you're done! You could also do the same thing for Zod, but it's not required right now, as it's the default validation library.
42+
43+
If you want more information about the TypeSchema to built-in system change, there's a dedicated discussion on GitHub for that, [here](https://github.com/TheEdoRan/next-safe-action/discussions/201).

website/docs/safe-action-client/instance-methods.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ metadata(data: Metadata) => new SafeActionClient()
3131
schema(schema: S, utils?: { handleValidationErrorsShape?: HandleValidationErrorsShapeFn } }) => new SafeActionClient()
3232
```
3333

34-
`schema` accepts an input schema of type `Schema` (from TypeSchema) or a function that returns a promise of type `Schema` and an optional `utils` object that accepts a [`handleValidationErrorsShape`](/docs/recipes/customize-validation-errors-format) function. The schema is used to define the arguments that the safe action will receive, the optional [`handleValidationErrorsShape`](/docs/recipes/customize-validation-errors-format) function is used to return a custom format for validation errors. If you don't pass an input schema, `parsedInput` and validation errors will be typed `undefined`, and `clientInput` will be typed `void`. It returns a new instance of the safe action client.
34+
`schema` accepts an input schema of type `Schema` or a function that returns a promise of type `Schema` and an optional `utils` object that accepts a [`handleValidationErrorsShape`](/docs/recipes/customize-validation-errors-format) function. The schema is used to define the arguments that the safe action will receive, the optional [`handleValidationErrorsShape`](/docs/recipes/customize-validation-errors-format) function is used to return a custom format for validation errors. If you don't pass an input schema, `parsedInput` and validation errors will be typed `undefined`, and `clientInput` will be typed `void`. It returns a new instance of the safe action client.
3535

3636
## `bindArgsSchemas`
3737

3838
```typescript
3939
bindArgsSchemas(bindArgsSchemas: BAS, bindArgsUtils?: { handleBindArgsValidationErrorsShape?: HandleBindArgsValidationErrorsShapeFn }) => new SafeActionClient()
4040
```
4141

42-
`bindArgsSchemas` accepts an array of bind input schemas of type `Schema[]` (from TypeSchema) and an optional `bindArgsUtils` object that accepts a `handleBindArgsValidationErrorsShape` function. The schema is used to define the bind arguments that the safe action will receive, the optional `handleBindArgsValidationErrorsShape` function is used to [return a custom format for bind arguments validation errors](/docs/recipes/customize-validation-errors-format). It returns a new instance of the safe action client.
42+
`bindArgsSchemas` accepts an array of bind input schemas of type `Schema[]` and an optional `bindArgsUtils` object that accepts a `handleBindArgsValidationErrorsShape` function. The schema is used to define the bind arguments that the safe action will receive, the optional `handleBindArgsValidationErrorsShape` function is used to [return a custom format for bind arguments validation errors](/docs/recipes/customize-validation-errors-format). It returns a new instance of the safe action client.
4343

4444
## `action` / `stateAction`
4545

0 commit comments

Comments
 (0)