Skip to content

Commit 35291ad

Browse files
authored
Clarify structure of nested errors in a custom resolver (#1160)
* Clarify structure of nested errors in a custom resolver The existing documentation infers that the return value of the `errors` key on a custom resolver should use flat keys that match the path of the error (since this is the convention throughout the lib), however it turns out that the internal code for RHF actually expects a hierarchy to be returned... kept me up till 3am last night tracking this one down 😅 - RHF is awesome! * Fix incomplete path in example snippet
1 parent e9499f9 commit 35291ad

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/content/docs/useform.mdx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,10 @@ npm install @hookform/resolvers
435435
- A resolver can not be used with the built-in validators (e.g.: required, min, etc.)
436436
- When building a custom resolver:
437437
- Make sure that you return an object with both `values` and `errors` properties. Their default values should be an empty object. For example: `{}`.
438-
- The keys of the `error` object should match the `name` values of your fields.
438+
- The keys of the `errors` object should match the `name` values of your fields, but they _must_ be hierarchical rather than a single key for deep errors:
439+
`❌ { "participants.1.name": someErr }` will not set or clear properly - instead, use `✅ { participants: [null, { name: someErr } ] }` as this is reachable
440+
as `errors.participants[1].name` - you can still prepare your errors using flat keys, and then use a function like this one from the zod resolver:
441+
[toNestErrors(flatErrs, resolverOptions)](https://github.com/react-hook-form/resolvers/blob/master/src/toNestErrors.ts)
439442

440443
</Admonition>
441444

0 commit comments

Comments
 (0)