Skip to content

Commit

Permalink
refactor: check server validation errors with `ActionServerValidation…
Browse files Browse the repository at this point in the history
…Error` instance
  • Loading branch information
TheEdoRan committed Jul 20, 2024
1 parent c3aba3b commit 99cba74
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 11 deletions.
11 changes: 2 additions & 9 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 {
StateServerCodeFn,
} from "./index.types";
import { ActionMetadataError, DEFAULT_SERVER_ERROR_MESSAGE, isError } from "./utils";
import { ActionValidationError, buildValidationErrors } from "./validation-errors";
import { ActionServerValidationError, ActionValidationError, buildValidationErrors } from "./validation-errors";
import type {
BindArgsValidationErrors,
HandleBindArgsValidationErrorsShapeFn,
Expand Down Expand Up @@ -223,14 +223,7 @@ export function actionBuilder<
}

// If error is `ActionServerValidationError`, return `validationErrors` as if schema validation would fail.
// TODO: check if this is still true after switching to built-in system
if (
e instanceof Error &&
"kind" in e &&
"validationErrors" in e &&
typeof e.kind === "string" &&
e.kind === "__actionServerValidationError"
) {
if (e instanceof ActionServerValidationError) {
const ve = e.validationErrors as ValidationErrors<S>;
middlewareResult.validationErrors = await Promise.resolve(args.handleValidationErrorsShape(ve));
} else {
Expand Down
2 changes: 0 additions & 2 deletions packages/next-safe-action/src/validation-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ 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 Action server validation error(s) occurred");
this.validationErrors = validationErrors;
this.kind = "__actionServerValidationError";
}
}

Expand Down

0 comments on commit 99cba74

Please sign in to comment.