payload icon indicating copy to clipboard operation
payload copied to clipboard

ValidationError.data stripped in production build — err.name minified to 'e', breaking Admin UI field highlighting

Open azed77 opened this issue 4 months ago • 1 comments

Describe the Bug

In development (next dev), when required fields are left empty and Save is clicked, the API returns a full ValidationError response with data.errors containing per-field error details:

{
  "errors": [
    {
      "name": "ValidationError",
      "data": {
        "collection": "topic-page",
        "errors": [
          {
            "label": "Meta > Title",
            "message": "This field is required.",
            "path": "title"
          }
        ]
      },
      "message": "The following fields are invalid: Meta > Title"
    }
  ]
}

In production (next build && next start), data is completely stripped from the response:

{
  "errors": [
    {
      "message": "The following fields are invalid: Meta > Title"
    }
  ]
}

This breaks the Payload Admin UI — the toast notification appears but inline field highlighting and per-field error messages are missing.

Root Cause (Confirmed)

Server-side logs in production show the error IS built correctly with full data.errors on the server: ERROR: Error: The following fields are invalid: Meta > Title

error: {

"data": {

"collection": "content",

"errors": [...]

},

"name": "e"   <-- minified class name

} 

Notice "name": "e" — the production minifier (Terser/SWC) is compressing the ValidationError class name to e.

Payload's error handler checks:

if (err.name === 'ValidationError')

In production, err.name === 'e', so this condition fails and the response falls through to the generic handler which only includes message, dropping data.errors entirely.

Current Workaround

Adding this to next.config.mjs restores the behavior:

webpack: (config, { isServer }) => {
  if (isServer) {
    const TerserPlugin = config.optimization.minimizer?.find(
      (p) => p.constructor.name === 'TerserPlugin'
    )
    if (TerserPlugin) {
      TerserPlugin.options.minimizer.options.keep_classnames = true
      TerserPlugin.options.minimizer.options.keep_fnames = true
    }
  }
  return config
},

Proposed Fix

The error handler should use a minification-proof check instead of relying on err.name string matching:

Option A — instanceof check (most robust):

if (err instanceof ValidationError)

Option B — structural fallback:

if (err.name === 'ValidationError' || (err.data && typeof err.data === 'object'))

Related Issues

Related to #7140 (same bug reported in beta, appears to have regressed in v3.84.1)

Link to the code that reproduces this issue

Reproduction Steps

  1. Create any collection with required fields
  2. Run next build && next start
  3. Open Admin UI, leave required fields empty, click Save
  4. Observe HTTP 400 response — data.errors is missing
  5. Compare with next dev — data.errors is present

Which area(s) are affected?

area: ui

Environment Info

- Payload CMS: 3.84.1
- @payloadcms/db-mongodb: 3.84.1
- Next.js: v16.2.6
- Node: 22.16.0
- Database: MongoDB / AWS DocumentDB

azed77 avatar Jul 13 '26 07:07 azed77

Please add a reproduction in order for us to be able to investigate.

Depending on the quality of reproduction steps, this issue may be closed if no reproduction is provided.

Why was this issue marked with the invalid-reproduction label?

To be able to investigate, we need access to a reproduction to identify what triggered the issue. We prefer a link to a public GitHub repository created with create-payload-app@latest -t blank or a forked/branched version of this repository with tests added (more info in the reproduction-guide).

To make sure the issue is resolved as quickly as possible, please make sure that the reproduction is as minimal as possible. This means that you should remove unnecessary code, files, and dependencies that do not contribute to the issue. Ensure your reproduction does not depend on secrets, 3rd party registries, private dependencies, or any other data that cannot be made public. Avoid a reproduction including a whole monorepo (unless relevant to the issue). The easier it is to reproduce the issue, the quicker we can help.

Please test your reproduction against the latest version of Payload to make sure your issue has not already been fixed.

I added a link, why was it still marked?

Ensure the link is pointing to a codebase that is accessible (e.g. not a private repository). "example.com", "n/a", "will add later", etc. are not acceptable links -- we need to see a public codebase. See the above section for accepted links.

Useful Resources

github-actions[bot] avatar Jul 13 '26 07:07 github-actions[bot]