Skip to content

Commit

Permalink
chore(website): add validation libraries support recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Jul 19, 2024
1 parent fcfb25a commit c3aba3b
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 46 deletions.
17 changes: 2 additions & 15 deletions website/docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ description: Getting started with next-safe-action version 7.
- Next.js >= 14 (>= 15 for [`useStateAction`](/docs/execution/hooks/usestateaction) hook)
- React >= 18.2.0
- TypeScript >= 5
- Zod or a validation library supported by [TypeSchema](https://typeschema.com/#coverage)
- Zod or Valibot or Yup
:::

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

:::note
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.

To use this feature, you just need to update the import path for the safe client initialization function from:
```typescript
import { createSafeActionClient } from "next-safe-action";
```

to:

```typescript
import { createSafeActionClient } from "next-safe-action/typeschema";
```

and install the related [TypeSchema adapter](https://typeschema.com/#coverage).
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.
:::

## Usage
Expand Down
2 changes: 1 addition & 1 deletion website/docs/recipes/additional-validation-errors.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 4
sidebar_position: 5
description: Set additional custom validation errors in schema or in action's server code function.
---

Expand Down
2 changes: 1 addition & 1 deletion website/docs/recipes/customize-validation-errors-format.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 5
sidebar_position: 6
description: Learn how to customize validation errors format returned to the client.
---

Expand Down
2 changes: 1 addition & 1 deletion website/docs/recipes/extend-base-client.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 2
sidebar_position: 3
description: Learn how to use both a basic and an authorization client at the same time in your project.
---

Expand Down
2 changes: 1 addition & 1 deletion website/docs/recipes/extend-previous-schema.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 3
sidebar_position: 4
description: Learn how to use next-safe-action with a i18n solution.
---

Expand Down
2 changes: 1 addition & 1 deletion website/docs/recipes/form-actions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 6
sidebar_position: 7
description: Learn how to use next-safe-action with Form Actions.
---

Expand Down
2 changes: 1 addition & 1 deletion website/docs/recipes/i18n.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 8
sidebar_position: 9
description: Learn how to use next-safe-action with a i18n solution.
---

Expand Down
2 changes: 1 addition & 1 deletion website/docs/recipes/upload-files.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 7
sidebar_position: 8
description: Learn how to upload a file using next-safe-action.
---

Expand Down
43 changes: 43 additions & 0 deletions website/docs/recipes/validation-libraries-support.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
sidebar_position: 2
description: Use a validation library of your choice with next-safe-action.
---

# Validation libraries support

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).

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.

If you used a TypeSchema adapter before, you should uninstall it, since you just need the validation library of your choice from now on.

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:


For Valibot:

```typescript title="@/lib/safe-action.ts"
import { createSafeActionClient } from "next-safe-action";
import { valibotAdapter } from "next-safe-action/adapters/valibot"; // import the adapter

export const actionClient = createSafeActionClient({
validationAdapter: valibotAdapter(), // <-- and then pass it to the client
// other options here...
});
```

For Yup:

```typescript title="@/lib/safe-action.ts"
import { createSafeActionClient } from "next-safe-action";
import { yupAdapter } from "next-safe-action/adapters/yup"; // import the adapter

export const actionClient = createSafeActionClient({
validationAdapter: yupAdapter(), // <-- and then pass it to the client
// other options here...
});
```

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.

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).
4 changes: 2 additions & 2 deletions website/docs/safe-action-client/instance-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ metadata(data: Metadata) => new SafeActionClient()
schema(schema: S, utils?: { handleValidationErrorsShape?: HandleValidationErrorsShapeFn } }) => new SafeActionClient()
```

`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.
`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.

## `bindArgsSchemas`

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

`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.
`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.

## `action` / `stateAction`

Expand Down
26 changes: 7 additions & 19 deletions website/docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,21 @@ description: Troubleshoot common issues with next-safe-action.

# Troubleshooting

## Common issues
## TypeSchema issues (pre v7.2.0)

### `Schema` and `parsedInput` are typed `any` (broken types) and build issues

At this time, TypeSchema (the library used under the hood for supporting multiple validation libraries) doesn't work with TypeScript >= 5.5; the resulting types for inputs and schemas are `any`, so type inference is broken.
**NOTE**: next-safe-action used TypeSchema up to version 7.1.3. If you use version 7.2.0 or later, these issues are fixed.

If you're in this situation, you have two paths to choose from to fix it:

1. If you're using a library other than Zod, you have to downgrade your TypeScript version to 5.4 and, assuming you're using VS Code, add the following to your `.vscode/settings.json`, to tell the editor to use the workspace version of TypeScript:
### `Schema` and `parsedInput` are typed `any` (broken types) and build issues

```json title=".vscode/settings.json"
{
"typescript.tsdk": "node_modules/typescript/lib"
}
```
At this time, TypeSchema (the library used under the hood up to v7.1.3 for supporting multiple validation libraries) doesn't work with TypeScript >= 5.5; the resulting types for inputs and schemas are `any`, so type inference is broken.

2. If you're using Zod and TypeScript 5.5, you can install the experimental version of next-safe-action. It shares the same codebase as the stable version, the only difference it's that it supports just Zod. More information about this can be found in [this issue](https://github.com/TheEdoRan/next-safe-action/issues/180#issuecomment-2201607407) on GitHub. You can install it in your project by running the following command:

```bash npm2yarn
npm i next-safe-action@experimental
```
If you're in this situation, please upgrade to v7.2.0 or later to fix the issue.

### TypeSchema issues with Edge Runtime

TypeSchema enables support for many validation libraries, via adapters. However, since it relies on the dynamic import feature, it won't work with the Edge Runtime, so you'll need to use the default Zod client if you want to render on the Edge.
TypeSchema enables support for many validation libraries, via adapters. However, since it relies on the dynamic import feature, it won't work with the Edge Runtime. Please upgrade to v7.2.0 or later to fix the issue.

### TypeScript error in monorepo
## TypeScript error in monorepo

If you use next-safe-action in a monorepo, you'll likely experience this error:

Expand Down
4 changes: 2 additions & 2 deletions website/docs/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,6 @@ type SchemaErrors<S> = {
---
## TypeSchema library
## Adapters types
`Infer`, `InferIn`, `Schema` types used in this documentation come from [TypeSchema](https://typeschema.com) library.
`Infer`, `InferIn`, `Schema` types used in this documentation come from `next-safe-action/adapters/types` path.
2 changes: 1 addition & 1 deletion website/src/components/landing/features.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const features: { title: string; description: string }[] = [
},
{
title: "Input validation using multiple validation libraries",
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>.`,
description: `Input passed from the client to the server is validated using Zod, Valibot or Yup.`,
},
{
title: "Advanced server error handling",
Expand Down

0 comments on commit c3aba3b

Please sign in to comment.