Skip to content

fix: throw clear error for non-existent listSearchableFields - #17714

Open
vjymisal0 wants to merge 1 commit into
payloadcms:mainfrom
vjymisal0:fix/listSearchableFields-invalid-field-14904
Open

fix: throw clear error for non-existent listSearchableFields#17714
vjymisal0 wants to merge 1 commit into
payloadcms:mainfrom
vjymisal0:fix/listSearchableFields-invalid-field-14904

Conversation

@vjymisal0

Copy link
Copy Markdown

What?

Adds validation that throws a clear InvalidConfiguration error when admin.listSearchableFields on a collection config references a field name that does not exist on that collection.

Why?

Fixes #14904.

Previously, referencing a non-existent field name in admin.listSearchableFields produced no error at all in pnpm dev, and in production builds it "imploded" the admin List View search with no usable error message - the actual failure ("The following path cannot be queried: <field>") only ever showed up in server logs, never reaching the user.

How?

Added validateListSearchableFields in packages/payload/src/collections/config/listSearchableFields.ts, mirroring the existing validateUseAsTitle precedent in the same directory. It flattens the collection's top-level fields and checks each entry in admin.listSearchableFields (skipping the implicit id field, and only checking the top-level segment of dotted/relationship paths) exists on the collection, throwing InvalidConfiguration naming the offending field and collection slug if not.

This is called from sanitizeCollection right after validateUseAsTitle, so it runs unconditionally at config-sanitization time in both dev and production - no more environment-dependent failure mode.

Test plan

Added packages/payload/src/collections/config/listSearchableFields.spec.ts (mirrors useAsTitle.spec.ts), covering:

  • throws on a non-existent field
  • does not throw when all fields exist
  • does not throw on the default id field
  • does not throw when listSearchableFields is undefined

Note: pnpm install in this sandbox environment was intermittently failing on Windows temp-path EPERM/network timeout issues while I prepared this PR, so I could not get a green local vitest run to paste here. The test code itself directly follows the existing, currently-passing useAsTitle.spec.ts pattern in the same file, using the same sanitizeCollection harness.

Fixes #14904

Previously, referencing a field name in admin.listSearchableFields
that does not exist on the collection would not surface any error
during development (pnpm dev). In production builds, the same
invalid config would cause the search query to hit the database with
an unqueryable path, "imploding" the admin List View search with no
usable error message to the user.

This adds validateListSearchableFields, called during collection
sanitization (mirroring the existing validateUseAsTitle pattern),
which eagerly throws an InvalidConfiguration error naming the
offending field and collection as soon as the config is loaded -
consistently in both dev and production.

Fixes payloadcms#14904
@vjymisal0

Copy link
Copy Markdown
Author

Update: dependencies finished installing in my sandbox, so I was able to run the tests locally after all.

npx vitest run packages/payload/src/collections/config/listSearchableFields.spec.ts packages/payload/src/collections/config/useAsTitle.spec.ts --project unit

 Test Files  2 passed (2)
      Tests  12 passed (12)

All 4 new tests pass, and the existing useAsTitle.spec.ts suite (whose pattern this PR's test mirrors) is unaffected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Having non-existent fields in listSearchableFields fails in a bad way, only in production builds

1 participant