Skip to content

Commit

Permalink
refactor(validation-errors): rename rootErrors to formErrors for …
Browse files Browse the repository at this point in the history
…flattened errors
  • Loading branch information
TheEdoRan committed Apr 11, 2024
1 parent e4aacbc commit 218176d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 5 additions & 4 deletions packages/next-safe-action/src/validation-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ export function returnValidationErrors<S extends Schema>(

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

Expand All @@ -96,7 +97,7 @@ export function flattenValidationErrors<

for (const [key, value] of Object.entries<string[] | { _errors: string[] }>(validationErrors)) {
if (key === "_errors" && Array.isArray(value)) {
flattened.rootErrors = [...value];
flattened.formErrors = [...value];
} else {
if ("_errors" in value) {
flattened.fieldErrors[key as keyof Omit<VE, "_errors">] = [...value._errors];
Expand Down
4 changes: 2 additions & 2 deletions packages/next-safe-action/src/validation-errors.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ export type BindArgsValidationErrors<BAS extends Schema[]> = (ValidationErrors<
> | null)[];

/**
* Type of flattened validation errors. `rootErrors` contains global errors, and `fieldErrors`
* Type of flattened validation errors. `formErrors` contains global errors, and `fieldErrors`
* contains errors for each field, one level deep.
*/
export type FlattenedValidationErrors<S extends Schema, VE extends ValidationErrors<S>> = {
rootErrors: string[];
formErrors: string[];
fieldErrors: {
[K in keyof Omit<VE, "_errors">]?: string[];
};
Expand Down

0 comments on commit 218176d

Please sign in to comment.