fix(db-mongodb): exists query on checkbox fields (#12567)

This commit is contained in:
Jarrod Flesch
2025-05-27 11:19:09 -04:00
committed by GitHub
parent b61ef13481
commit f2b6c4a707
4 changed files with 60 additions and 1 deletions

View File

@@ -417,7 +417,7 @@ export const sanitizeQueryValue = ({
return buildExistsQuery(
formattedValue,
path,
!['relationship', 'upload'].includes(field.type),
!['checkbox', 'relationship', 'upload'].includes(field.type),
)
}
}

View File

@@ -10,6 +10,10 @@ const CheckboxFields: CollectionConfig = {
type: 'checkbox',
required: true,
},
{
name: 'checkboxNotRequired',
type: 'checkbox',
},
],
}

View File

@@ -30,6 +30,7 @@ import { clearAndSeedEverything } from './seed.js'
import {
arrayFieldsSlug,
blockFieldsSlug,
checkboxFieldsSlug,
collapsibleFieldsSlug,
groupFieldsSlug,
relationshipFieldsSlug,
@@ -1349,6 +1350,58 @@ describe('Fields', () => {
})
})
describe('checkbox', () => {
beforeEach(async () => {
await payload.delete({
collection: checkboxFieldsSlug,
where: {
id: {
exists: true,
},
},
})
})
it('should query checkbox fields with exists operator', async () => {
const existsTrueDoc = await payload.create({
collection: checkboxFieldsSlug,
data: {
checkbox: true,
checkboxNotRequired: false,
},
})
const existsFalseDoc = await payload.create({
collection: checkboxFieldsSlug,
data: {
checkbox: true,
},
})
const existsFalse = await payload.find({
collection: checkboxFieldsSlug,
where: {
checkboxNotRequired: {
exists: false,
},
},
})
expect(existsFalse.totalDocs).toBe(1)
expect(existsFalse.docs[0]?.id).toEqual(existsFalseDoc.id)
const existsTrue = await payload.find({
collection: checkboxFieldsSlug,
where: {
checkboxNotRequired: {
exists: true,
},
},
})
expect(existsTrue.totalDocs).toBe(1)
expect(existsTrue.docs[0]?.id).toEqual(existsTrueDoc.id)
})
})
describe('unique indexes', () => {
it('should throw validation error saving on unique fields', async () => {
const data = {

View File

@@ -730,6 +730,7 @@ export interface TextField {
export interface CheckboxField {
id: string;
checkbox: boolean;
checkboxNotRequired?: boolean | null;
updatedAt: string;
createdAt: string;
}
@@ -2318,6 +2319,7 @@ export interface LocalizedTabsBlockSelect<T extends boolean = true> {
*/
export interface CheckboxFieldsSelect<T extends boolean = true> {
checkbox?: T;
checkboxNotRequired?: T;
updatedAt?: T;
createdAt?: T;
}