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
@@ -83,14 +96,15 @@ export const lists = {
8396 successMany : 'Voted for {countSuccess} {singular|plural}' ,
8497 } ,
8598 itemView : {
99+ actionMode : { disabled : { hidden : { equals : true } } } ,
86100 hidePrompt : true ,
87101 } ,
88102 listView : {
89103 actionMode : 'hidden' ,
90104 } ,
91105 } ,
92- } ,
93- report : {
106+ } ) ,
107+ report : action ( {
94108 access : allowAll ,
95109 async resolve ( { actionKey, where } , context ) {
96110 console . log ( `${ actionKey } ` , JSON . stringify ( { where } ) )
@@ -130,7 +144,30 @@ export const lists = {
130144 actionMode : { disabled : { hidden : { equals : true } } } ,
131145 } ,
132146 } ,
133- } ,
147+ } ) ,
148+ updateWithoutValidation : action ( {
149+ access : allowAll ,
150+ async resolve ( { where, data } , context ) {
151+ return context
152+ . withSession ( { ...context . session , noValidation : true } )
153+ . db . Post . updateOne ( { where, data } )
154+ } ,
155+ graphql : {
156+ singular : 'updatePostWithoutValidation' ,
157+ plural : 'updatePostsWithoutValidation' ,
158+ __data : true ,
159+ } ,
160+ ui : {
161+ label : 'Save without validation' ,
162+ itemView : {
163+ navigation : 'refetch' ,
164+ hideToast : true ,
165+ } ,
166+ listView : {
167+ actionMode : { disabled : { hidden : { equals : true } } } ,
168+ } ,
169+ } ,
170+ } ) ,
134171 } ,
135172 ui : {
136173 listView : {
@@ -141,4 +178,4 @@ export const lists = {
141178 } ,
142179 } ,
143180 } ) ,
144- } satisfies Lists
181+ } satisfies Lists < { noValidation ?: boolean } >
0 commit comments