Skip to content

Commit f8519cc

Browse files
committedOct 21, 2024··
refactor: clean up some logic
1 parent ea4687b commit f8519cc

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed
 

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

+4-8
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {
1414
ServerCodeFn,
1515
StateServerCodeFn,
1616
} from "./index.types";
17-
import { DEFAULT_SERVER_ERROR_MESSAGE, isError } from "./utils";
17+
import { DEFAULT_SERVER_ERROR_MESSAGE, isError, winningBoolean } from "./utils";
1818
import type { MaybePromise } from "./utils.types";
1919
import {
2020
ActionMetadataValidationError,
@@ -327,13 +327,9 @@ export function actionBuilder<
327327
const actionResult: SafeActionResult<ServerError, IS, BAS, CVE, CBAVE, Data> = {};
328328

329329
if (typeof middlewareResult.validationErrors !== "undefined") {
330-
// Throw validation errors if either `throwValidationErrors` property at the action or instance level is `true`.
331-
// If `throwValidationErrors` property at the action is `false`, do not throw validation errors, since it
332-
// has a higher priority than the instance one.
333-
if (
334-
(utils?.throwValidationErrors || args.throwValidationErrors) &&
335-
utils?.throwValidationErrors !== false
336-
) {
330+
// `utils.throwValidationErrors` has higher priority since it's set at the action level.
331+
// It overrides the client setting, if set.
332+
if (winningBoolean(args.throwValidationErrors, utils?.throwValidationErrors)) {
337333
throw new ActionValidationError(middlewareResult.validationErrors as CVE);
338334
} else {
339335
actionResult.validationErrors = middlewareResult.validationErrors as CVE;
+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
export const DEFAULT_SERVER_ERROR_MESSAGE = "Something went wrong while executing the operation.";
22

3+
/**
4+
* Checks if passed argument is an instance of Error.
5+
*/
36
export const isError = (error: unknown): error is Error => error instanceof Error;
7+
8+
/**
9+
* Checks what the winning boolean value is from a series of values, from lowest to highest priority.
10+
* `null` and `undefined` values are skipped.
11+
*/
12+
export const winningBoolean = (...args: (boolean | undefined | null)[]) => {
13+
return args.reduce((acc, v) => (typeof v === "boolean" ? v : acc), false) as boolean;
14+
};

0 commit comments

Comments
 (0)
Please sign in to comment.