File tree Expand file tree Collapse file tree 4 files changed +59
-1
lines changed
packages/payload/src/fields/hooks/beforeValidate
collections/BeforeValidate Expand file tree Collapse file tree 4 files changed +59
-1
lines changed Original file line number Diff line number Diff line change @@ -282,7 +282,7 @@ export const promise = async <T>({
282
282
overrideAccess,
283
283
path : fieldPath ,
284
284
previousSiblingDoc : siblingDoc ,
285
- previousValue : siblingData [ field . name ] ,
285
+ previousValue : siblingDoc [ field . name ] ,
286
286
req,
287
287
schemaPath : fieldSchemaPath ,
288
288
siblingData,
Original file line number Diff line number Diff line change @@ -16,5 +16,36 @@ export const BeforeValidateCollection: CollectionConfig = {
16
16
] ,
17
17
} ,
18
18
} ,
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
+ } ,
19
50
] ,
20
51
}
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ import {
21
21
import { relationsSlug } from './collections/Relations/index.js'
22
22
import { transformSlug } from './collections/Transform/index.js'
23
23
import { hooksUsersSlug } from './collections/Users/index.js'
24
+ import { beforeValidateSlug } from './collectionSlugs.js'
24
25
import { HooksConfig } from './config.js'
25
26
import { dataHooksGlobalSlug } from './globals/Data/index.js'
26
27
@@ -526,4 +527,28 @@ describe('Hooks', () => {
526
527
expect ( body ) . toEqual ( { errors : [ { message : "I'm a teapot" } ] } )
527
528
} )
528
529
} )
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
+ } )
529
554
} )
Original file line number Diff line number Diff line change @@ -84,6 +84,7 @@ export interface HooksUserAuthOperations {
84
84
export interface BeforeValidate {
85
85
id : string ;
86
86
title ?: string | null ;
87
+ selection ?: ( 'a' | 'b' ) | null ;
87
88
updatedAt : string ;
88
89
createdAt : string ;
89
90
}
@@ -318,6 +319,7 @@ export interface PayloadMigration {
318
319
*/
319
320
export interface BeforeValidateSelect < T extends boolean = true > {
320
321
title ?: T ;
322
+ selection ?: T ;
321
323
updatedAt ?: T ;
322
324
createdAt ?: T ;
323
325
}
You can’t perform that action at this time.
0 commit comments