Repository navigation
fix: support nested validation error paths in useForm - #890
Conversation
Validators report nested messages as dotted keys such as `profile.name` or `contacts.0.email`. The error map, `setError()` and `clearErrors()` now take those keys, so apps no longer need a cast to read or clear them.
Editing or replacing a parent object or array now clears the errors of the fields below it, matching how editing a scalar clears that field's error. Clearing a parent with `clearErrors()` does the same and keeps unrelated errors. Svelte snapshots the parent field when a handler reports a nested error, because the parent is what changed in `data`.
|
👁️🗨️ Review this pull request with grouped, summarized diffs at: raw result💭 analyzed by {
"headSha": "8f80909bf4522271ad4e899bdd95b3730aba4b3b",
"result": {
"overallSummary": "`useForm()` only understood **top-level** body field names: a nested validator message like `profile.name` was neither typed (TS7053 when read) nor cleared when the parent value was replaced or `clearErrors('profile')` was called, so stale messages survived a fix. This PR makes error keys a `FormErrorPath` union derived from the action body, adds shared `isFormErrorWithin`/`omitFormErrors` helpers, and uses them in all four adapters so a replaced or cleared parent also drops the errors of its nested fields.",
"groups": [
{
"key": "error-path-types",
"label": "Nested error path types & helpers",
"summary": "Foundation of the fix: `FormErrorPath` walks the action body as a dotted template-literal union (including numeric array segments) and replaces `keyof TData` in `FormErrors`, `setError()` and `clearErrors()`. Two pure helpers, `isFormErrorWithin` and `omitFormErrors`, encode \"this key belongs to that field\" so every adapter clears parents and children the same way.",
"category": "core",
"filePaths": [
"packages/void/src/pages/form-presentation.ts",
"packages/void/src/pages/client.ts"
],
"fileNotes": [
{
"path": "packages/void/src/pages/form-presentation.ts",
"text": "Public type change: existing consumers that index `errors` with a plain `string` may now see stricter typing, since the record keys are a union rather than `keyof TData & string`."
}
],
"lineNotes": [
{
"path": "packages/void/src/pages/form-presentation.ts",
"side": "additions",
"line": 27,
"text": "Recursion stops by resolving to `never` at depth 6, so a body nested deeper than that silently loses its nested error keys instead of producing an expensive or infinite type."
},
{
"path": "packages/void/src/pages/form-presentation.ts",
"side": "additions",
"line": 30,
"text": "Array segments are typed as `` `${number}` ``, so `contacts.0.email` is accepted but so is an out-of-range `contacts.99.email` — indices are not checked against the tuple length."
},
{
"path": "packages/void/src/pages/form-presentation.ts",
"side": "additions",
"line": 94,
"text": "Returning the *same* object when nothing matched is load-bearing: React's `setErrorEdits` compares by reference to skip a state update."
}
]
},
{
"key": "adapter-clearing",
"label": "Adapters clear nested errors",
"summary": "Wires the shared helpers into each framework adapter so editing a scalar or replacing a parent value drops every error key below it, and `clearErrors(field)` also clears nested keys. React and Solid delegate to `omitFormErrors`; Vue and Svelte mutate the existing error map in place, as their `'in-place'` editing idiom requires.",
"category": "core",
"filePaths": [
"packages/react/src/runtime/use-form.ts",
"packages/vue/src/runtime/use-form.ts",
"packages/svelte/src/runtime/use-form.svelte.ts",
"packages/solid/src/runtime/use-form.ts"
],
"fileNotes": [
{
"path": "packages/svelte/src/runtime/use-form.svelte.ts",
"text": "Input handlers report a nested error before the change effect runs, so `snapshotField()` now snapshots the *parent* field (`field.split('.')[0]`) — the value that actually changes in `data` — otherwise the parent change is re-detected and the freshly set nested error is cleared again."
}
],
"lineNotes": [
{
"path": "packages/react/src/runtime/use-form.ts",
"side": "additions",
"line": 205,
"text": "The early-return condition changed from `!(field in current)` to a reference check on `omitFormErrors`: previously `clearErrors`/edit of `profile` returned early when only `profile.name` existed, which is exactly the bug being fixed."
}
]
},
{
"key": "tests",
"label": "Regression & type tests",
"summary": "One runtime case per adapter sets `profile.name` and `contacts.0.email`, replaces/clears the parents, and asserts only the unrelated error remains; four `checkNestedErrorPaths()` typecheck functions pin the nested keys as readable and clearable while `profile.missing` and `contacts.first` stay rejected inside `@ts-expect-error`.",
"category": "tests",
"filePaths": [
"packages/react/test/unit/use-form.test.ts",
"packages/react/test/unit/use-form.typecheck.ts",
"packages/vue/test/unit/use-form.test.ts",
"packages/vue/test/unit/form-errors.typecheck.ts",
"packages/svelte/test/unit/use-form.test.ts",
"packages/svelte/test/unit/form-errors.typecheck.ts",
"packages/solid/test/unit/form-errors.test.ts",
"packages/solid/test/unit/form-errors.typecheck.ts"
],
"fileNotes": [
{
"path": "packages/solid/test/unit/form-errors.test.ts",
"text": "`createForm()` gained `server`/`defaults` parameters with backwards-compatible defaults so the new case can seed nested server errors without a second fixture."
},
{
"path": "packages/svelte/test/unit/use-form.test.ts",
"text": "Cases call `svelte.flushSync()` between edits because the error clearing runs in an effect, not synchronously in the setter."
}
]
},
{
"key": "docs",
"label": "Docs for nested error paths",
"summary": "Updates the API table and the `setError()` guidance to say error keys can be dotted nested paths and that editing or clearing a parent field also clears the errors of its nested fields.",
"category": "docs",
"filePaths": [
"docs/guide/pages-routing/actions-and-forms.md"
]
}
],
"schemaVersion": 1,
"source": "llm",
"generatedAt": "2026-11-21T02:34:06.868Z",
"model": "openai-compatible/opencode-go/deepseek-v4.1-flash",
"locale": "en"
}
} |
|
Thanks! It looks like there are a few issues still. Here is a slop review:
|
|
Thank you! For the second one I'd rather fix the semantics than the prefix check in each adapter. For the types, I'll keep top-level keys in the union: For clearing, I want one shared helper that diffs the previous and next value and returns the leaf paths that actually changed, used by all four adapters. Editing One assertion in the new tests will change. Replacing a parent only clears the leaves that changed, so I'll add cases for sibling errors and the dotted field name. |
The nested path union is a deferred conditional for an unresolved type parameter, so generic helpers keyed by `keyof T & string` no longer indexed the error map.
|
Assigning |
0e9b24f to
9f3cf5c
Compare
|
There are only two shapes here, and it's a trade-off. Now it keeps exact nested keys, and a wrapper holding an error map types it as |
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
# Conflicts: # packages/react/src/runtime/use-form.ts # packages/solid/src/runtime/use-form.ts # packages/svelte/src/runtime/use-form.svelte.ts # packages/vue/src/runtime/use-form.ts
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
- Stop walking a body type where it repeats an ancestor, and past six segments, by accepting any nested path there. A body with several self references no longer fails with TS2589, and deep validator keys such as `sections.0.rows.0.cells.0.value` are accepted. - Treat files, blobs, dates, maps, sets and branded primitives as one field, so `avatar.size` or `email.length` are not error paths. - Compare ancestors by identity, so an all-optional child object that only matches its parent keeps rejecting typos. - Fix lint rules enabled on main since the branch was opened. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Fixes #875
Description
useForm()now accepts and clears the dotted error keys that nested validators return, in React, Vue, Svelte and Solid.The error map,
setError()andclearErrors()are typed by aFormErrorPathunion that walks the action body.profile.nameandcontacts.0.emailare valid keys, while a field that does not exist is still a type error. Top-level keys stay in the union, so generic helpers withK extends keyof T & stringkeep compiling. Files, blobs, dates, maps, sets and branded strings count as one field. Where a recursive body type repeats, or past six segments, any nested path is accepted, so a body with several self references stays cheap to check.Editing data clears only the errors of the values that changed, plus the errors of their parent fields. Errors on unchanged siblings stay, for example on other contacts, and a literal field named
email.workis kept apart fromemail.clearErrors('profile')clearsprofileand every error below it.Type change
FormErrors<T>is now keyed by nested paths. Inside a generic function,Partial<Record<keyof T & string, string>>is no longer assignable toFormState<T, ...>['errors']: TypeScript cannot prove the nested-path keys from a genericT. Type such a map asFormErrors<T>, whichvoid/pages-clientnow exports. Concrete body types are not affected.Test
clears only the errors of the fields a change touchedinpackages/{react,vue,svelte}/test/unit/use-form.test.tsandpackages/solid/test/unit/form-errors.test.ts.packages/svelte/test/unit/use-form.test.ts:keeps an error set for a field whose name contains a dotandpreserves a separate literal dotted error when editing or clearing its prefix.packages/solid/test/unit/form-errors.test.ts:keeps an untouched row error when clearing its parent array error.packages/void/test/unit/form-presentation.test.ts: the shared helpers, including literal dotted fields and missing optional parents.checkNestedErrorPaths()in each adapter's typecheck file, andpackages/void/test/unit/form-error-path.typecheck.tsfor typos, leaf values, paths deeper than six segments, recursive bodies and all-optional child objects.