fix: querying virtual fields deeply with draft: true (#12868)

Fixes an issue when querying deeply new relationship virtual fields with
`draft: true`. Changes the method for `where` sanitization, before it
was done in `validateSearchParam` which didn't work with versions
properly, now there's a separate `sanitizeWhereQuery` function that does
this.
This commit is contained in:
Sasha
2025-06-24 05:18:49 +03:00
committed by GitHub
parent bb17cc3ea8
commit bc9b501e28
11 changed files with 108 additions and 5 deletions

View File

@@ -38,6 +38,7 @@ export default buildConfigWithDefaults({
collections: [
{
slug: 'categories',
versions: { drafts: true },
fields: [
{
type: 'text',

View File

@@ -2222,6 +2222,25 @@ describe('database', () => {
expect(found.docs[0].id).toBe(doc.id)
})
it('should allow to query by virtual field 2x deep with draft:true', async () => {
const category = await payload.create({
collection: 'categories',
data: { title: '3-category' },
})
const post = await payload.create({
collection: 'posts',
data: { title: '3-post', category: category.id },
})
const doc = await payload.create({ collection: 'virtual-relations', data: { post: post.id } })
const found = await payload.find({
collection: 'virtual-relations',
where: { postCategoryTitle: { equals: '3-category' } },
draft: true,
})
expect(found.docs).toHaveLength(1)
expect(found.docs[0].id).toBe(doc.id)
})
it('should allow referenced virtual field in globals', async () => {
const post = await payload.create({ collection: 'posts', data: { title: 'post' } })
const globalData = await payload.updateGlobal({