plugin-nested-docs: child validation error during resaveChildren silently rolls back the parent update while responding with success (data loss + misleading 'Document modified' modal)
Describe the Bug
When updating a parent page in a collection that uses @payloadcms/plugin-nested-docs, the plugin's resaveChildren hook re-saves all child documents inside the same transaction. If one of the children fails validation during this re-save (in our case: a required upload field whose media document had been deleted in the meantime), the following happens:
- The plugin logs the error (
Nested Docs plugin encountered an error while re-saving a child document.) but does not rethrow it to the caller. - The API response for the parent update reports success, including the new
updatedAtof the parent. - The surrounding transaction is rolled back — the parent update never reaches the database.
Result: silent data loss. Every save of the parent appears to succeed but is discarded.
In the admin UI this manifests in a particularly confusing way: after the (seemingly successful) save, the stale-data check re-arms with the updatedAt from the success response, the next form-state roundtrip compares it against the (rolled-back, old) server value, and the editor gets the “Document modified” (documentModified) modal — suggesting another user changed the document. Reloading discards their edit; repeat forever. Editors cannot save the parent page at all and never see the actual validation error, which lives on a grandchild document.
Reproduction Steps
- Collection
pages(drafts disabled, localized fields, postgres adapter) withplugin-nested-docs(breadcrumbs). - Create parent page A with child page B. Give B a block containing a required
uploadfield, populated with a media doc. - Delete the referenced media document (media delete does not re-validate referencing docs, so B now has a dangling required upload).
- Update any field on parent A (REST or Local API).
Expected: either the update succeeds and persists, or it fails with a validation error surfaced to the caller.
Actual: the update responds 200 with a fresh updatedAt, the error is only logged server-side, the transaction is rolled back, and the database still contains the old state. In the admin UI, the editor gets the misleading “Document modified” modal after every save attempt.
Verified via Local API: payload.update() resolves successfully with a new updatedAt, while a raw SQL read of pages.updated_at immediately afterwards still returns the old value.
Stack trace (logged, not thrown)
ERROR: Nested Docs plugin encountered an error while re-saving a child document.
ValidationError: The following field is invalid: … > File
at beforeChange (payload/src/fields/hooks/beforeChange/index.ts:77:11)
at async updateDocument (payload/src/collections/operations/utilities/update.ts:278:28)
at async updateByIDOperation (payload/src/collections/operations/updateByID.ts:203:18)
at async <anonymous> (@payloadcms/plugin-nested-docs/src/hooks/resaveChildren.ts:72:25)
at async updateDocument (payload/src/collections/operations/utilities/update.ts:462:10)
at async updateByIDOperation (payload/src/collections/operations/updateByID.ts:203:18)
at async <anonymous> (@payloadcms/plugin-nested-docs/src/hooks/resaveChildren.ts:72:25)
(The double frames come from the recursive parent → child → grandchild cascade; the failing document was a grandchild.)
Suggested direction
Either of these would avoid the silent data loss:
resaveChildrenshould skip re-validation for untouched children (or run withoverrideValidation), since the re-save only rewrites breadcrumbs/paths; or- if the child re-save fails, the error should propagate so the parent update fails loudly instead of responding success on a rolled-back transaction.
Environment Info
payload: 3.85.2
@payloadcms/db-postgres: 3.85.2
@payloadcms/plugin-nested-docs: 3.85.2
@payloadcms/next: 3.85.2
Node: 22.x
Database: PostgreSQL 17 (Neon)
next: 16.x
Disclosure: this bug was analyzed with AI assistance (Claude). All core claims were verified empirically against a copy of our production database — successful payload.update() response vs. immediate raw-SQL read showing the old value, a control update on an unaffected sibling document persisting fine, and the logged (not thrown) ValidationError from resaveChildren in the server output. Happy to provide a minimal reproduction repository on request — one is prepared.
Sharper root cause after further analysis — the problematic code is the catch in resaveChildren:
} catch (err) {
req.payload.logger.error(`Nested Docs plugin encountered an error while re-saving a child document.`);
req.payload.logger.error(err);
if (err instanceof ValidationError && err.data?.errors?.length) {
throw new APIError('Could not publish or save changes: One or more children are invalid.', 400);
}
}
Two independent silent-data-loss paths:
- The rethrow is conditional on
err instanceof ValidationError. In bundled/dual-module environments (Next.js server bundle, tsx dev loaders), the plugin's importedValidationErrorclass can be a different copy than the one the child update throws —instanceofreturnsfalse, the error is swallowed, and the parent update responds with success on a transaction that subsequently rolls back. This is exactly what we observe in our production app (error appears in the logs, API returns 200 with a freshupdatedAt, database keeps the old state), while a pristine minimal setup takes the rethrow path and fails loudly. - Any non-
ValidationError(e.g. a DB error during the child re-save) is always swallowed by this catch — same doomed-transaction/success-response combination.
Suggested fix: rethrow unconditionally (optionally wrapping ValidationErrors in a friendlier APIError), and/or make the type check structural (err?.name === 'ValidationError') instead of instanceof.
A minimal reproduction repository is prepared; note that a pristine single-module setup reproduces the conditional variant (loud failure), while path 1 requires a bundled context — happy to share either on request.
Corroborating this (we hit it at 3.80.0 and work around it in production), and adding a third swallow path that requires no bundled/dual-module context — it may explain your production observation directly, since your stack trace shows the failing document is a grandchild.
The conditional rethrow in the catch doesn't rethrow the child's ValidationError — it converts it to an APIError:
if (err instanceof ValidationError && err.data?.errors?.length) {
throw new APIError('Could not publish or save changes: One or more children are invalid.', 400);
}
When the cascade is more than one level deep, that APIError propagates into the next ancestor's resaveChildren, whose identical catch now sees err instanceof ValidationError === false by construction — and swallows it. So error information survives at most one cascade level, even in a pristine single-module setup:
- parent A → child B fails validation:
resaveChildren(A)catches theValidationError, rethrows asAPIError→ loud failure. ✔️ - parent A → child B → grandchild C fails validation:
update(C)throwsValidationError→resaveChildren(B)converts it toAPIError→ that propagates out ofupdate(B)intoresaveChildren(A)'s catch → not aValidationError→ swallowed → A's update responds success on the doomed transaction. ❌
This matches the double resaveChildren frames in your trace exactly.
One implication for the fix: making the type check structural (err?.name === 'ValidationError') is not sufficient on its own — the first level still converts to APIError, so the second level swallows it regardless of how the check is written. Rethrowing unconditionally is the variant that closes all three paths.
Workaround we run in the meantime, in case it helps others: since error propagation through the cascade can't be trusted, we verify instead — a collection afterChange hook appended after the plugin's (the plugin prepends its hooks, so ours runs post-cascade, on the editor's own save) that walks the entire stored subtree breadth-first and checks each descendant is consistent with its parent (in our case: that each child's materialized path extends its parent's). Anything stale throws a ValidationError — which both fails the editor's save loudly instead of committing a half-cascaded state, and, when it fires inside a cascaded child save, is of the exact type the direct ancestor's catch does recognize and escalate.
Another data point: a database error during a child re-save, not only a ValidationError, triggers the same swallow. On Postgres the error aborts the transaction, the catch swallows it, the final COMMIT becomes a ROLLBACK, and the caller gets a success response for a save that never landed. That's path 1 in #17701's description, and it needs no invalid data to reach.
We replace resaveChildren with a hook that rethrows, and that passes a grandchild's error through unchanged so the message names the page that actually failed. +1 for getting #17701 reviewed.