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
@@ -38,9 +38,22 @@ const isReportDisabled = {
3838export const lists = {
3939 Post : list ( {
4040 access : allowAll , // WARNING: public
41+
4142 fields : {
4243 title : text ( ) ,
43- content : text ( ) ,
44+ content : text ( {
45+ hooks : {
46+ validate : args => {
47+ if (
48+ typeof args . resolvedFieldData === 'string' &&
49+ ! args . context . session ?. noValidation &&
50+ ! args . resolvedFieldData . includes ( 'good content' )
51+ ) {
52+ args . addValidationError ( 'Content is not valid' )
53+ }
54+ } ,
55+ } ,
56+ } ) ,
4457 hidden : checkbox ( {
4558 ui : {
4659 itemView : {
@@ -57,7 +70,7 @@ export const lists = {
5770 reportedAt : timestamp ( { ...readOnly } ) ,
5871 } ,
5972 actions : {
60- vote : {
73+ vote : action ( {
6174 access : ( { context } ) => {
6275 const ua = context . req ?. headers [ 'user-agent' ] ?? ''
6376 // only allow voting from Chrome browsers
@@ -87,14 +100,15 @@ export const lists = {
87100 successMany : 'Voted for {countSuccess} {singular|plural}' ,
88101 } ,
89102 itemView : {
103+ actionMode : { disabled : { hidden : { equals : true } } } ,
90104 hidePrompt : true ,
91105 } ,
92106 listView : {
93107 actionMode : 'hidden' ,
94108 } ,
95109 } ,
96- } ,
97- report : {
110+ } ) ,
111+ report : action ( {
98112 access : allowAll ,
99113 async resolve ( { actionKey, where } , context ) {
100114 console . log ( `${ actionKey } ` , JSON . stringify ( { where } ) )
@@ -136,7 +150,30 @@ export const lists = {
136150 actionMode : { disabled : isReportDisabled } ,
137151 } ,
138152 } ,
139- } ,
153+ } ) ,
154+ updateWithoutValidation : action ( {
155+ access : allowAll ,
156+ async resolve ( { where, data } , context ) {
157+ return context
158+ . withSession ( { ...context . session , noValidation : true } )
159+ . db . Post . updateOne ( { where, data } )
160+ } ,
161+ graphql : {
162+ singular : 'updatePostWithoutValidation' ,
163+ plural : 'updatePostsWithoutValidation' ,
164+ __data : true ,
165+ } ,
166+ ui : {
167+ label : 'Save without validation' ,
168+ itemView : {
169+ navigation : 'refetch' ,
170+ hideToast : true ,
171+ } ,
172+ listView : {
173+ actionMode : { disabled : { hidden : { equals : true } } } ,
174+ } ,
175+ } ,
176+ } ) ,
140177 } ,
141178 ui : {
142179 listView : {
@@ -147,4 +184,4 @@ export const lists = {
147184 } ,
148185 } ,
149186 } ) ,
150- } satisfies Lists
187+ } satisfies Lists < { noValidation ?: boolean } >
0 commit comments