Skip to content

Commit

Permalink
fix: clearable point fields in both mongo and postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
r1tsuu committed Nov 29, 2024
1 parent ef875d5 commit 702091e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/payload/src/fields/hooks/beforeValidate/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ export const promise = async <T>({

case 'point': {
if (Array.isArray(siblingData[field.name])) {
if ((siblingData[field.name] as string[]).some((val) => val === null || val === '')) {
siblingData[field.name] = null
break
}

siblingData[field.name] = (siblingData[field.name] as string[]).map((coordinate, i) => {
if (typeof coordinate === 'string') {
const value = siblingData[field.name][i] as string
Expand Down
7 changes: 6 additions & 1 deletion packages/payload/src/fields/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,12 @@ export type PointFieldValidation = Validate<
PointField
>

export const point: PointFieldValidation = (value = ['', ''], { req: { t }, required }) => {
export const point: PointFieldValidation = (value, { req: { t }, required }) => {
// Allow to pass null to clear the field
if (!value) {
value = ['', '']
}

const lng = parseFloat(String(value[0]))
const lat = parseFloat(String(value[1]))
if (
Expand Down
24 changes: 24 additions & 0 deletions test/fields/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,30 @@ describe('Fields', () => {
expect(doc.localized).toEqual(localized)
expect(doc.group).toMatchObject(group)
})

it('should clear a point field', async () => {
if (payload.db.name === 'sqlite') {
return
}

const doc = await payload.create({
collection: 'point-fields',
data: {
point: [7, -7],
group: {
point: [7, -7],
},
},
})

const res = await payload.update({
collection: 'point-fields',
id: doc.id,
data: { group: { point: null } },
})

expect(res.group.point).toBeFalsy()
})
})

describe('unique indexes', () => {
Expand Down

0 comments on commit 702091e

Please sign in to comment.