-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validation-errors): support flattening via `flattenValidationErr…
…ors` function (#100) Sometimes it's better to deal with a flattened error object instead of a formatted one, for instance when you don't need to use nested objects in validation schemas. This PR exports a function called `flattenValidationErrors` that does what it says. Be aware that it works just one level deep, as it discards nested schema errors. This is a known limitation of this approach, since it can't prevent key conflicts. Suppose this is a returned formatted `validationErrors` object: ```typescript validationErrors = { _errors: ["Global error"], username: { _errors: ["Too short", "Username is invalid"], }, email: { _errors: ["Email is invalid"], } } ``` After passing it to `flattenValidationErrors`: ```typescript import { flattenValidationErrors } from "next-safe-action"; const flattenedErrors = flattenValidationErrors(validationErrors); ``` It becomes this: ```typescript flattenedErrors = { rootErrors: ["Global error"], fieldErrors: { username: ["Too short", "Username is invalid"], email: ["Email is invalid"], } } ```
- Loading branch information
Showing
3 changed files
with
53 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters