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
refactor: pass utils to validation errors shape customizer functions (#263)
Code in this PR adds a second argument to `handleValidationErrorsShape`
and `handleBindArgsValidationErrorsShape` functions, called `utils`,
which is an object that contains `clientInput`, `bindArgsClientInputs`,
`metadata` and `ctx` properties. This addition allows you to set dynamic
validation errors based on current action execution data.
re #256
Copy file name to clipboardexpand all lines: website/docs/define-actions/validation-errors.md
+5-3
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@ This can be customized both at the safe action client level and at the action le
13
13
- using [`defaultValidationErrorsShape`](/docs/define-actions/create-the-client#defaultvalidationerrorsshape) optional property in `createSafeActionClient`;
14
14
- using `handleValidationErrorsShape` and `handleBindArgsValidationErrorsShape` optional functions in [`schema`](/docs/define-actions/instance-methods#schema) and [`bindArgsSchemas`](/docs/define-actions/instance-methods#bindargsschemas) methods.
15
15
16
-
The second way overrides the shape set at the instance level, per action. More information below.
16
+
The second way overrides the shape set at the instance level, per action.
17
17
18
18
For example, if you want to flatten the validation errors (emulation of Zod's [`flatten`](https://zod.dev/ERROR_HANDLING?id=flattening-errors) method), you can (but not required to) use the `flattenValidationErrors` utility function exported from the library, combining it with `handleValidationErrorsShape` inside `schema` method:
The second argument of both `handleValidationErrorsShape` and `handleBindArgsValidationErrors` functions is an `utils` object that contains info about the current action execution (`clientInput`, `bindArgsClientInputs`, `metadata` and `ctx` properties). It's passed to the functions to allow granular and dynamic customization of the validation errors shape.
56
+
55
57
:::note
56
58
If you chain multiple `schema` methods, as explained in the [Extend previous schema](/docs/define-actions/extend-previous-schemas) page, and want to override the default validation errors shape, you **must** use `handleValidationErrorsShape` inside the last `schema` method, otherwise there would be a type mismatch in the returned action result.
0 commit comments