Skip to content

Commit 99ca1ba

Browse files
fix: beforeValidate previousValue argument (#10022)
### What? `previousValue` was incorrect. It would always return the current value. ### Why? It was accessing siblingData instead of siblingDoc. Other hooks use siblingDoc, but this one was using siblingData.
1 parent 13e0505 commit 99ca1ba

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

packages/payload/src/fields/hooks/beforeValidate/promise.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ export const promise = async <T>({
282282
overrideAccess,
283283
path: fieldPath,
284284
previousSiblingDoc: siblingDoc,
285-
previousValue: siblingData[field.name],
285+
previousValue: siblingDoc[field.name],
286286
req,
287287
schemaPath: fieldSchemaPath,
288288
siblingData,

test/hooks/collections/BeforeValidate/index.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,36 @@ export const BeforeValidateCollection: CollectionConfig = {
1616
],
1717
},
1818
},
19+
{
20+
type: 'select',
21+
name: 'selection',
22+
options: [
23+
{
24+
label: 'A',
25+
value: 'a',
26+
},
27+
{
28+
label: 'B',
29+
value: 'b',
30+
},
31+
],
32+
hooks: {
33+
beforeValidate: [
34+
({ value, previousValue, context }) => {
35+
if (context.beforeValidateTest) {
36+
if (value !== 'a') {
37+
return 'beforeValidate value is incorrect'
38+
}
39+
40+
if (previousValue !== 'b') {
41+
return 'beforeValidate previousValue is incorrect'
42+
}
43+
44+
return value
45+
}
46+
},
47+
],
48+
},
49+
},
1950
],
2051
}

test/hooks/int.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
import { relationsSlug } from './collections/Relations/index.js'
2222
import { transformSlug } from './collections/Transform/index.js'
2323
import { hooksUsersSlug } from './collections/Users/index.js'
24+
import { beforeValidateSlug } from './collectionSlugs.js'
2425
import { HooksConfig } from './config.js'
2526
import { dataHooksGlobalSlug } from './globals/Data/index.js'
2627

@@ -526,4 +527,28 @@ describe('Hooks', () => {
526527
expect(body).toEqual({ errors: [{ message: "I'm a teapot" }] })
527528
})
528529
})
530+
531+
describe('beforeValidate', () => {
532+
it('should have correct arguments', async () => {
533+
const doc = await payload.create({
534+
collection: beforeValidateSlug,
535+
data: {
536+
selection: 'b',
537+
},
538+
})
539+
540+
const updateResult = await payload.update({
541+
id: doc.id,
542+
collection: beforeValidateSlug,
543+
data: {
544+
selection: 'a',
545+
},
546+
context: {
547+
beforeValidateTest: true,
548+
},
549+
})
550+
551+
expect(updateResult).toBeDefined()
552+
})
553+
})
529554
})

test/hooks/payload-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export interface HooksUserAuthOperations {
8484
export interface BeforeValidate {
8585
id: string;
8686
title?: string | null;
87+
selection?: ('a' | 'b') | null;
8788
updatedAt: string;
8889
createdAt: string;
8990
}
@@ -318,6 +319,7 @@ export interface PayloadMigration {
318319
*/
319320
export interface BeforeValidateSelect<T extends boolean = true> {
320321
title?: T;
322+
selection?: T;
321323
updatedAt?: T;
322324
createdAt?: T;
323325
}

0 commit comments

Comments
 (0)