Skip to content

Commit

Permalink
fix(validation-errors): server validation errors were incorrectly pro…
Browse files Browse the repository at this point in the history
…cessed by typeschema clients
  • Loading branch information
TheEdoRan committed Jun 5, 2024
1 parent b0457a0 commit cd567a2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
12 changes: 10 additions & 2 deletions packages/next-safe-action/src/action-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {
} from "./index.types";
import type { InferArray } from "./utils";
import { ActionMetadataError, DEFAULT_SERVER_ERROR_MESSAGE, isError, zodValidate } from "./utils";
import { ActionServerValidationError, buildValidationErrors } from "./validation-errors";
import { buildValidationErrors } from "./validation-errors";
import type {
BindArgsValidationErrors,
HandleBindArgsValidationErrorsShapeFn,
Expand Down Expand Up @@ -212,7 +212,15 @@ export function actionBuilder<
}

// If error is `ActionServerValidationError`, return `validationErrors` as if schema validation would fail.
if (e instanceof ActionServerValidationError) {
// Shouldn't be this difficult to check for `ActionServerValidationError`, but /typeschema clients fail
// if it's not done this way.
if (
e instanceof Error &&
"kind" in e &&
"validationErrors" in e &&
typeof e.kind === "string" &&
e.kind === "__actionServerValidationError"
) {
const ve = e.validationErrors as ValidationErrors<S>;
middlewareResult.validationErrors = await Promise.resolve(args.handleValidationErrorsShape(ve));
} else {
Expand Down
6 changes: 3 additions & 3 deletions packages/next-safe-action/src/typeschema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export type * from "./validation-errors.types";

/**
* Create a new safe action client.
* This client supports multiple validation libraries via [TypeSchema](https://typeschema.com). If you experience
* issues when using this, switch to the main client exported from `next-safe-action`, which just supports Zod.
* Note: this client only works with Zod as the validation library.
* If you want to use a validation library supported by [TypeSchema](https://typeschema.com), import this client from `/typeschema` path.
* @param createOpts Optional initialization options
*
* {@link https://next-safe-action.dev/docs/safe-action-client/initialization-options See docs for more information}
Expand Down Expand Up @@ -56,7 +56,7 @@ export const createSafeActionClient = <
middlewareFns: [async ({ next }) => next({ ctx: undefined })],
handleServerErrorLog,
handleReturnedServerError,
validationStrategy: "zod",
validationStrategy: "typeschema",
schema: undefined,
bindArgsSchemas: [],
ctxType: undefined,
Expand Down
4 changes: 3 additions & 1 deletion packages/next-safe-action/src/validation-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ export const buildValidationErrors = <S extends Schema | undefined>(issues: Vali
// This class is internally used to throw validation errors in action's server code function, using
// `returnValidationErrors`.
export class ActionServerValidationError<S extends Schema> extends Error {
public kind: string;
public validationErrors: ValidationErrors<S>;
constructor(validationErrors: ValidationErrors<S>) {
super("Server Validation Error");
super("Action server validation error");
this.validationErrors = validationErrors;
this.kind = "__actionServerValidationError";
}
}

Expand Down

0 comments on commit cd567a2

Please sign in to comment.