Skip to content

Commit 218176d

Browse files
committedApr 11, 2024··
refactor(validation-errors): rename rootErrors to formErrors for flattened errors
1 parent e4aacbc commit 218176d

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed
 

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ export function returnValidationErrors<S extends Schema>(
7676

7777
/**
7878
* Transform default formatted validation errors into flattened structure.
79-
* `rootErrors` contains global errors, and `fieldErrors` contains errors for each field,
80-
* one level deep. It skips errors for nested fields.
79+
* `formErrors` contains global errors, and `fieldErrors` contains errors for each field,
80+
* one level deep. It discards errors for nested fields.
81+
* Emulation of `zod`'s [`flatten`](https://zod.dev/ERROR_HANDLING?id=flattening-errors) function.
8182
* @param {ValidationErrors} [validationErrors] Validation errors object
8283
* @returns {FlattenedValidationErrors} Flattened validation errors
8384
*/
@@ -86,7 +87,7 @@ export function flattenValidationErrors<
8687
const VE extends ValidationErrors<S>,
8788
>(validationErrors?: VE) {
8889
const flattened: FlattenedValidationErrors<S, VE> = {
89-
rootErrors: [],
90+
formErrors: [],
9091
fieldErrors: {},
9192
};
9293

@@ -96,7 +97,7 @@ export function flattenValidationErrors<
9697

9798
for (const [key, value] of Object.entries<string[] | { _errors: string[] }>(validationErrors)) {
9899
if (key === "_errors" && Array.isArray(value)) {
99-
flattened.rootErrors = [...value];
100+
flattened.formErrors = [...value];
100101
} else {
101102
if ("_errors" in value) {
102103
flattened.fieldErrors[key as keyof Omit<VE, "_errors">] = [...value._errors];

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ export type BindArgsValidationErrors<BAS extends Schema[]> = (ValidationErrors<
2626
> | null)[];
2727

2828
/**
29-
* Type of flattened validation errors. `rootErrors` contains global errors, and `fieldErrors`
29+
* Type of flattened validation errors. `formErrors` contains global errors, and `fieldErrors`
3030
* contains errors for each field, one level deep.
3131
*/
3232
export type FlattenedValidationErrors<S extends Schema, VE extends ValidationErrors<S>> = {
33-
rootErrors: string[];
33+
formErrors: string[];
3434
fieldErrors: {
3535
[K in keyof Omit<VE, "_errors">]?: string[];
3636
};

0 commit comments

Comments
 (0)
Please sign in to comment.