Skip to content

fix: validate "null" value for point field as true when its not required #12908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions packages/payload/src/fields/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,19 +937,27 @@ export type PointFieldValidation = Validate<
>

export const point: PointFieldValidation = (value = ['', ''], { req: { t }, required }) => {
const lng = parseFloat(String(value![0]))
const lat = parseFloat(String(value![1]))
if (value === null) {
if (required) {
return t('validation:required')
}

return true
}

const lng = parseFloat(String(value[0]))
const lat = parseFloat(String(value[1]))
if (
required &&
((value![0] && value![1] && typeof lng !== 'number' && typeof lat !== 'number') ||
((value[0] && value[1] && typeof lng !== 'number' && typeof lat !== 'number') ||
Number.isNaN(lng) ||
Number.isNaN(lat) ||
(Array.isArray(value) && value.length !== 2))
) {
return t('validation:requiresTwoNumbers')
}

if ((value![1] && Number.isNaN(lng)) || (value![0] && Number.isNaN(lat))) {
if ((value[1] && Number.isNaN(lng)) || (value[0] && Number.isNaN(lat))) {
return t('validation:invalidInput')
}

Expand Down
52 changes: 52 additions & 0 deletions test/fields/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,58 @@ describe('Fields', () => {
expect(doc.localized).toEqual(localized)
expect(doc.group).toMatchObject(group)
})

it('should throw validation error when "required" field is set to null', async () => {
if (payload.db.name === 'sqlite') {
return
}
// first create the point field
doc = await payload.create({
collection: 'point-fields',
data: {
localized,
point,
},
})

// try to update the required field to null
await expect(() =>
payload.update({
collection: 'point-fields',
data: {
point: null,
},
id: doc.id,
}),
).rejects.toThrow('The following field is invalid: Location')
})

it('should not throw validation error when non-"required" field is set to null', async () => {
if (payload.db.name === 'sqlite') {
return
}
// first create the point field
doc = await payload.create({
collection: 'point-fields',
data: {
localized,
point,
},
})

expect(doc.localized).toEqual(localized)

// try to update the non-required field to null
const updatedDoc = await payload.update({
collection: 'point-fields',
data: {
localized: null,
},
id: doc.id,
})

expect(updatedDoc.localized).toEqual(undefined)
})
})

describe('checkbox', () => {
Expand Down
Loading