You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/define-actions/action-utils.md
+13-5Lines changed: 13 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ description: Action utils is an object with useful properties and callbacks func
5
5
6
6
# Action utils
7
7
8
-
Action utils is an object with some useful properties and callbacks passed as the second argument of the [`action`/`stateAction`](/docs/define-actions/instance-methods#action--stateaction) method.
8
+
Action utils is an object with some useful properties and callbacks passed as the second argument of the [`action()`/`stateAction()`](/docs/define-actions/instance-methods#action--stateaction) method.
9
9
10
10
## Throw errors when they occur
11
11
@@ -14,11 +14,12 @@ Starting from v7.4.0, you can now pass optional `throwServerError` and `throwVal
14
14
15
15
## Action callbacks
16
16
17
-
-`onSuccess`: called when action execution succeeds.
18
-
-`onError`: called when action execution fails (validation errors or server error).
19
-
-`onSettled`: called when action execution succeeds or fails.
17
+
-`onSuccess()`: called when action execution succeeds.
18
+
-`onError()`: called when action execution fails (validation errors or server error).
19
+
-`onNavigation()`: called when a `next/navigation` function is called in a middleware function or in the action's server code function.
20
+
-`onSettled()`: called when action execution succeeds or fails.
20
21
21
-
With action callbacks you can perform custom logic after the action is executed, on the server side. You can pass them to [`action`/`stateAction`](/docs/define-actions/instance-methods#action--stateaction) method as the second argument, after the server code function. Their return value is not used and they **must** be async functions.
22
+
With action callbacks you can perform custom logic after the action is executed, on the server side. You can pass them to [`action()`/`stateAction()`](/docs/define-actions/instance-methods#action--stateaction) method as the second argument, after the server code function. Their return value is not used and they **must** be async functions.
description: Learn how to use next-safe-action with a i18n solution.
3
+
description: Learn how to extend previous schema(s)
4
4
---
5
5
6
6
# Extend previous schema(s)
7
7
8
-
Sometimes it's useful to define an action "template" with a base schema and then extend it with additional properties. This can be done inside the [`schema`](/docs/define-actions/instance-methods#schema) method by passing an async function that has the previous schema as its argument. See the example below:
8
+
Sometimes it's useful to define an action "template" with a base input schema and then extend it with additional properties. This can be done inside the [`inputSchema`](/docs/define-actions/instance-methods#inputschema) method by passing an async function that has the previous schema as its argument. See the example below:
9
9
10
10
```typescript
11
11
"use server";
12
12
13
13
import { actionClient } from"@/lib/safe-action";
14
14
import { z } from"zod";
15
15
16
-
constschema=z.object({
16
+
constinputSchema=z.object({
17
17
username: z.string(),
18
18
});
19
19
20
20
const myAction =actionClient
21
-
.schema(schema)
21
+
.inputSchema(inputSchema)
22
22
// highlight-start
23
-
.schema(async (prevSchema) => {
23
+
.inputSchema(async (prevSchema) => {
24
24
// Here we extend the previous schema with `password` property.
Copy file name to clipboardExpand all lines: website/docs/define-actions/validation-errors.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ description: Learn how to customize or manually creating validation errors.
10
10
next-safe-action, by default, emulates Zod's [`format()`](https://zod.dev/ERROR_HANDLING?id=formatting-errors) method for building both validation and bind args validation errors and return them to the client.
11
11
12
12
This can be customized both at the safe action client level and at the action level by:
13
-
- using [`defaultValidationErrorsShape`](/docs/define-actions/create-the-client#defaultvalidationerrorsshape) optional property in `createSafeActionClient`;
13
+
- using [`defaultValidationErrorsShape`](/docs/define-actions/create-the-client#defaultvalidationerrorsshape) optional property in `createSafeActionClient()`;
14
14
- using `handleValidationErrorsShape()` optional async function in [`inputSchema()`](/docs/define-actions/instance-methods#inputschema) method.
15
15
16
16
The second way overrides the shape set at the instance level, per action.
@@ -90,7 +90,7 @@ When input data fails schema validation, a `validationErrors` object is returned
90
90
91
91
It's often useful to also define custom logic to set additional validation errors by ourselves, for example when a user is signing up and password/confirm password fields don't match, and/or when the email is already in use.
92
92
93
-
Let's see how to implement this specific case in the optimal way, using both schema refinements and errors set in action's server code function, thanks to `returnValidationErrors`.
93
+
Let's see how to implement this specific case in the optimal way, using both schema refinements and errors set in action's server code function, thanks to `returnValidationErrors()`.
- You're required to pass a schema as the first argument of `returnValidationErrors`. This is used to infer the type of the validation errors set via the second argument.
148
-
- Errors set using `returnValidationErrors` will not be merged with the schema ones. If schema validation fails, the execution stops before reaching action's server code function. Otherwise, the action's backend code would receive invalid parsed input.
147
+
- You're required to pass a schema as the first argument of `returnValidationErrors()`. This is used to infer the type of the validation errors set via the second argument.
148
+
- Errors set using `returnValidationErrors()` will not be merged with the schema ones. If schema validation fails, the execution stops before reaching action's server code function. Otherwise, the action's backend code would receive invalid parsed input.
149
149
-`returnValidationErrors()` returns `never`. This means that internally it throws an error that gets caught and processed by next-safe-action, so code declared below the `returnValidationErrors()` invocation will not be executed.
150
150
- Since it returns `never`, you don't need to use `return` before this function call, and you can call it only once per execution path (it works the same way as Next.js `redirect()` and `notFound()` functions).
Copy file name to clipboardExpand all lines: website/docs/migrations/v7-to-v8.md
+24-22Lines changed: 24 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,8 +11,8 @@ Version 8 introduces significant changes to the validation system, improves type
11
11
Legend:
12
12
- ⚠️ Breaking change
13
13
- 🆕 New feature
14
-
- ⏳ Deprecation
15
14
- ✨ Improvement
15
+
- 🔄 Refactor
16
16
17
17
## What's new?
18
18
@@ -41,7 +41,8 @@ The behavior when using functions from `next/navigation` was very unclear and co
41
41
42
42
This behavior has been changed in v8. Now, when you're using functions imported from `next/navigation` in an action:
43
43
- the hooks `status` value will be `"hasNavigated"` instead of `"hasSucceeded"`;
44
-
- a new `onNavigation` callback will be triggered, both for actions and hooks, instead of `onSuccess`. This callback receives a `navigationKind` value, that indicates the type of navigation that occurred.
44
+
- a new `onNavigation` callback will be triggered, both for actions and hooks, instead of `onSuccess`. This callback receives a `navigationKind` value, that indicates the type of navigation that occurred;
45
+
- the `success` property of the middleware result will now be `false`, instead of `true`, if a navigation function was called in a middleware function or in the action's server code function.
@@ -91,24 +92,6 @@ const result = await boundAction(input);
91
92
92
93
The deprecated `executeOnMount` hook functionality has been removed in v8. Server Actions should be used only for mutations, so it doesn't make sense to execute them on mount. Or at least, it shouldn't be a common case and, above all, a library job. If you still need to do it, just use `useEffect` to trigger the execution, however you want.
93
94
94
-
### ⏳ `schema` method renamed to `inputSchema`
95
-
96
-
The library, since version 7.8.0, supports both input and output validation, respectively using the `schema` and `outputSchema` methods. In v8, the `schema` method has been renamed to `inputSchema` to better reflect its purpose, and avoid potential confusion.
97
-
98
-
The `schema` method is deprecated and will be removed in a future version, but it's still available for backward compatibility. It's now just an alias for `inputSchema`:
99
-
100
-
```typescript title="v7"
101
-
actionClient.schema(/* ... */)
102
-
```
103
-
104
-
```typescript title="v8"
105
-
actionClient.inputSchema(/* ... */)
106
-
```
107
-
108
-
:::info UPDATE EXISTING ACTIONS
109
-
To update your actions, you can just use the search and replace feature of your editor to replace all occurrences of `.schema` with `.inputSchema`.
110
-
:::
111
-
112
95
### ✨ Type-checked metadata
113
96
114
97
This is a big improvement in type safety over v7. Metadata is now statically type-checked when passed to actions. So, now if you forget to pass the expected metadata shape, as defined by the `defineMetadataSchema` init option, you will get a type error immediately:
@@ -144,7 +127,7 @@ const action = actionClient
144
127
);
145
128
```
146
129
147
-
### ✨ Safe action result always defined
130
+
### 🔄 Safe action result always defined
148
131
149
132
The action result object is now always defined. This allows you to destructure it without the need to check if it's defined or not first:
150
133
@@ -164,7 +147,26 @@ if (result?.data) {
164
147
const { data } =awaitaction(input);
165
148
```
166
149
167
-
### Deprecated `useStateAction` hook
150
+
### 🔄 `schema` method renamed to `inputSchema`
151
+
152
+
The library, since version 7.8.0, supports both input and output validation, respectively using the `schema` and `outputSchema` methods. In v8, the `schema` method has been renamed to `inputSchema` to better reflect its purpose, and avoid potential confusion.
153
+
154
+
The `schema` method is deprecated and will be removed in a future version, but it's still available for backward compatibility. It's now just an alias for `inputSchema`:
155
+
156
+
```typescript title="v7"
157
+
actionClient.schema(/* ... */)
158
+
```
159
+
160
+
```typescript title="v8"
161
+
actionClient.inputSchema(/* ... */)
162
+
```
163
+
164
+
:::info UPDATE EXISTING ACTIONS
165
+
To update your actions, you can just use the search and replace feature of your editor to replace all occurrences of `.schema` with `.inputSchema`.
166
+
:::
167
+
168
+
169
+
### 🔄 Deprecation of `useStateAction` hook
168
170
169
171
The `useStateAction` hook has been deprecated. It's always been kind of a hack to begin with, and it doesn't support progressive enhancement, since it tries to do what the `useAction` and `useOptimisticAction` hooks do.
0 commit comments