Skip to content

Commit cd567a2

Browse files
committedJun 5, 2024··
fix(validation-errors): server validation errors were incorrectly processed by typeschema clients
1 parent b0457a0 commit cd567a2

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed
 

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {
1414
} from "./index.types";
1515
import type { InferArray } from "./utils";
1616
import { ActionMetadataError, DEFAULT_SERVER_ERROR_MESSAGE, isError, zodValidate } from "./utils";
17-
import { ActionServerValidationError, buildValidationErrors } from "./validation-errors";
17+
import { buildValidationErrors } from "./validation-errors";
1818
import type {
1919
BindArgsValidationErrors,
2020
HandleBindArgsValidationErrorsShapeFn,
@@ -212,7 +212,15 @@ export function actionBuilder<
212212
}
213213

214214
// If error is `ActionServerValidationError`, return `validationErrors` as if schema validation would fail.
215-
if (e instanceof ActionServerValidationError) {
215+
// Shouldn't be this difficult to check for `ActionServerValidationError`, but /typeschema clients fail
216+
// if it's not done this way.
217+
if (
218+
e instanceof Error &&
219+
"kind" in e &&
220+
"validationErrors" in e &&
221+
typeof e.kind === "string" &&
222+
e.kind === "__actionServerValidationError"
223+
) {
216224
const ve = e.validationErrors as ValidationErrors<S>;
217225
middlewareResult.validationErrors = await Promise.resolve(args.handleValidationErrorsShape(ve));
218226
} else {

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export type * from "./validation-errors.types";
2323

2424
/**
2525
* Create a new safe action client.
26-
* This client supports multiple validation libraries via [TypeSchema](https://typeschema.com). If you experience
27-
* issues when using this, switch to the main client exported from `next-safe-action`, which just supports Zod.
26+
* Note: this client only works with Zod as the validation library.
27+
* If you want to use a validation library supported by [TypeSchema](https://typeschema.com), import this client from `/typeschema` path.
2828
* @param createOpts Optional initialization options
2929
*
3030
* {@link https://next-safe-action.dev/docs/safe-action-client/initialization-options See docs for more information}
@@ -56,7 +56,7 @@ export const createSafeActionClient = <
5656
middlewareFns: [async ({ next }) => next({ ctx: undefined })],
5757
handleServerErrorLog,
5858
handleReturnedServerError,
59-
validationStrategy: "zod",
59+
validationStrategy: "typeschema",
6060
schema: undefined,
6161
bindArgsSchemas: [],
6262
ctxType: undefined,

‎packages/next-safe-action/src/validation-errors.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,12 @@ export const buildValidationErrors = <S extends Schema | undefined>(issues: Vali
5353
// This class is internally used to throw validation errors in action's server code function, using
5454
// `returnValidationErrors`.
5555
export class ActionServerValidationError<S extends Schema> extends Error {
56+
public kind: string;
5657
public validationErrors: ValidationErrors<S>;
5758
constructor(validationErrors: ValidationErrors<S>) {
58-
super("Server Validation Error");
59+
super("Action server validation error");
5960
this.validationErrors = validationErrors;
61+
this.kind = "__actionServerValidationError";
6062
}
6163
}
6264

0 commit comments

Comments
 (0)
Please sign in to comment.