Skip to content

Commit f2b6c4a

Browse files
fix(db-mongodb): exists query on checkbox fields (#12567)
1 parent b61ef13 commit f2b6c4a

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

packages/db-mongodb/src/queries/sanitizeQueryValue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ export const sanitizeQueryValue = ({
417417
return buildExistsQuery(
418418
formattedValue,
419419
path,
420-
!['relationship', 'upload'].includes(field.type),
420+
!['checkbox', 'relationship', 'upload'].includes(field.type),
421421
)
422422
}
423423
}

test/fields/collections/Checkbox/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const CheckboxFields: CollectionConfig = {
1010
type: 'checkbox',
1111
required: true,
1212
},
13+
{
14+
name: 'checkboxNotRequired',
15+
type: 'checkbox',
16+
},
1317
],
1418
}
1519

test/fields/int.spec.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { clearAndSeedEverything } from './seed.js'
3030
import {
3131
arrayFieldsSlug,
3232
blockFieldsSlug,
33+
checkboxFieldsSlug,
3334
collapsibleFieldsSlug,
3435
groupFieldsSlug,
3536
relationshipFieldsSlug,
@@ -1349,6 +1350,58 @@ describe('Fields', () => {
13491350
})
13501351
})
13511352

1353+
describe('checkbox', () => {
1354+
beforeEach(async () => {
1355+
await payload.delete({
1356+
collection: checkboxFieldsSlug,
1357+
where: {
1358+
id: {
1359+
exists: true,
1360+
},
1361+
},
1362+
})
1363+
})
1364+
1365+
it('should query checkbox fields with exists operator', async () => {
1366+
const existsTrueDoc = await payload.create({
1367+
collection: checkboxFieldsSlug,
1368+
data: {
1369+
checkbox: true,
1370+
checkboxNotRequired: false,
1371+
},
1372+
})
1373+
1374+
const existsFalseDoc = await payload.create({
1375+
collection: checkboxFieldsSlug,
1376+
data: {
1377+
checkbox: true,
1378+
},
1379+
})
1380+
1381+
const existsFalse = await payload.find({
1382+
collection: checkboxFieldsSlug,
1383+
where: {
1384+
checkboxNotRequired: {
1385+
exists: false,
1386+
},
1387+
},
1388+
})
1389+
expect(existsFalse.totalDocs).toBe(1)
1390+
expect(existsFalse.docs[0]?.id).toEqual(existsFalseDoc.id)
1391+
1392+
const existsTrue = await payload.find({
1393+
collection: checkboxFieldsSlug,
1394+
where: {
1395+
checkboxNotRequired: {
1396+
exists: true,
1397+
},
1398+
},
1399+
})
1400+
expect(existsTrue.totalDocs).toBe(1)
1401+
expect(existsTrue.docs[0]?.id).toEqual(existsTrueDoc.id)
1402+
})
1403+
})
1404+
13521405
describe('unique indexes', () => {
13531406
it('should throw validation error saving on unique fields', async () => {
13541407
const data = {

test/fields/payload-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,7 @@ export interface TextField {
730730
export interface CheckboxField {
731731
id: string;
732732
checkbox: boolean;
733+
checkboxNotRequired?: boolean | null;
733734
updatedAt: string;
734735
createdAt: string;
735736
}
@@ -2318,6 +2319,7 @@ export interface LocalizedTabsBlockSelect<T extends boolean = true> {
23182319
*/
23192320
export interface CheckboxFieldsSelect<T extends boolean = true> {
23202321
checkbox?: T;
2322+
checkboxNotRequired?: T;
23212323
updatedAt?: T;
23222324
createdAt?: T;
23232325
}

0 commit comments

Comments
 (0)