1- import { list } from '@keystone-6/core'
1+ import { list , action } from '@keystone-6/core'
22import { allowAll , denyAll } from '@keystone-6/core/access'
33import { checkbox , integer , text , timestamp } from '@keystone-6/core/fields'
44
@@ -34,9 +34,22 @@ const readOnly = {
3434export const lists = {
3535 Post : list ( {
3636 access : allowAll , // WARNING: public
37+
3738 fields : {
3839 title : text ( ) ,
39- content : text ( ) ,
40+ content : text ( {
41+ hooks : {
42+ validate : args => {
43+ if (
44+ typeof args . resolvedFieldData === 'string' &&
45+ ! args . context . session ?. noValidation &&
46+ ! args . resolvedFieldData . includes ( 'good content' )
47+ ) {
48+ args . addValidationError ( 'Content is not valid' )
49+ }
50+ } ,
51+ } ,
52+ } ) ,
4053 hidden : checkbox ( {
4154 ui : {
4255 itemView : {
@@ -53,7 +66,7 @@ export const lists = {
5366 reportedAt : timestamp ( { ...readOnly } ) ,
5467 } ,
5568 actions : {
56- vote : {
69+ vote : action ( {
5770 access : ( { context } ) => {
5871 const ua = context . req ?. headers [ 'user-agent' ] ?? ''
5972 // only allow voting from Chrome browsers
@@ -89,8 +102,8 @@ export const lists = {
89102 actionMode : 'hidden' ,
90103 } ,
91104 } ,
92- } ,
93- report : {
105+ } ) ,
106+ report : action ( {
94107 access : allowAll ,
95108 async resolve ( { actionKey, where } , context ) {
96109 console . log ( `${ actionKey } ` , JSON . stringify ( { where } ) )
@@ -130,7 +143,30 @@ export const lists = {
130143 actionMode : 'enabled' ,
131144 } ,
132145 } ,
133- } ,
146+ } ) ,
147+ updateWithoutValidation : action ( {
148+ access : allowAll ,
149+ async resolve ( { where, data } , context ) {
150+ return context
151+ . withSession ( { ...context . session , noValidation : true } )
152+ . db . Post . updateOne ( { where, data } )
153+ } ,
154+ graphql : {
155+ singular : 'updatePostWithoutValidation' ,
156+ plural : 'updatePostsWithoutValidation' ,
157+ __data : true ,
158+ } ,
159+ ui : {
160+ label : 'Save without validation' ,
161+ itemView : {
162+ navigation : 'refetch' ,
163+ hideToast : true ,
164+ } ,
165+ listView : {
166+ actionMode : 'enabled' ,
167+ } ,
168+ } ,
169+ } ) ,
134170 } ,
135171 ui : {
136172 listView : {
@@ -141,4 +177,4 @@ export const lists = {
141177 } ,
142178 } ,
143179 } ) ,
144- } satisfies Lists
180+ } satisfies Lists < { noValidation ?: boolean } >
0 commit comments