Skip to content

Commit

Permalink
Fix types for validateInput hook arguments on update operations (#8819)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Cousens <413395+dcousens@users.noreply.github.com>
  • Loading branch information
acburdine and dcousens authored Sep 20, 2023
1 parent aa18279 commit 52337e3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-lemons-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': patch
---

Fixes hooks.validateInput argument types for update operations
12 changes: 4 additions & 8 deletions examples/hooks/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Post {
id: ID!
title: String
content: String
feedback: String
preventDelete: Boolean
createdBy: String
createdAt: DateTime
Expand All @@ -25,6 +26,7 @@ input PostWhereInput {
id: IDFilter
title: StringFilter
content: StringFilter
feedback: StringFilter
preventDelete: BooleanFilter
createdBy: StringFilter
createdAt: DateTimeNullableFilter
Expand Down Expand Up @@ -91,6 +93,7 @@ input PostOrderByInput {
id: OrderDirection
title: OrderDirection
content: OrderDirection
feedback: OrderDirection
preventDelete: OrderDirection
createdBy: OrderDirection
createdAt: OrderDirection
Expand All @@ -106,11 +109,8 @@ enum OrderDirection {
input PostUpdateInput {
title: String!
content: String!
feedback: String
preventDelete: Boolean
createdBy: String
createdAt: DateTime
updatedBy: String
updatedAt: DateTime
}

input PostUpdateArgs {
Expand All @@ -122,10 +122,6 @@ input PostCreateInput {
title: String! = ""
content: String! = ""
preventDelete: Boolean
createdBy: String
createdAt: DateTime
updatedBy: String
updatedAt: DateTime
}

"""
Expand Down
1 change: 1 addition & 0 deletions examples/hooks/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ model Post {
id String @id @default(cuid())
title String @default("")
content String @default("")
feedback String @default("")
preventDelete Boolean @default(false)
createdBy String @default("")
createdAt DateTime?
Expand Down
35 changes: 32 additions & 3 deletions examples/hooks/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const readOnly = {
create: denyAll,
update: denyAll,
},
graphql: {
omit: {
create: true,
update: true,
},
},
ui: {
createView: {
fieldMode: (args: unknown) => 'hidden' as const,
Expand All @@ -33,11 +39,29 @@ export const lists: Lists = {
// we use isNonNull for these fields to enforce that they are always provided, and validated against a content filter
title: text({
validation: { isRequired: true },
graphql: { isNonNull: { create: true, update: true } },
graphql: {
isNonNull: {
create: true,
update: true,
},
},
}),
content: text({
validation: { isRequired: true },
graphql: { isNonNull: { create: true, update: true } },
graphql: {
isNonNull: {
create: true,
update: true,
},
},
}),
feedback: text({
validation: { isRequired: true },
graphql: {
omit: {
create: true,
},
},
}),
preventDelete: checkbox(),

Expand Down Expand Up @@ -106,9 +130,14 @@ export const lists: Lists = {
return resolvedData;
},
},
validateInput: ({ context, inputData, addValidationError }) => {
validateInput: ({ context, operation, inputData, addValidationError }) => {
const { title, content } = inputData;

if (operation === 'update' && 'feedback' in inputData) {
const { feedback } = inputData;
if (/profanity/i.test(feedback ?? '')) return addValidationError('Unacceptable feedback');
}

// an example of a content filter, the prevents the title or content containing the word "Profanity"
if (/profanity/i.test(title)) return addValidationError('Unacceptable title');
if (/profanity/i.test(content)) return addValidationError('Unacceptable content');
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/types/config/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,11 @@ type ValidateHook<
/**
* The GraphQL input **before** default values are applied
*/
inputData: ListTypeInfo['inputs']['create'];
inputData: ListTypeInfo['inputs']['update'];
/**
* The GraphQL input **after** being resolved by the field type's input resolver
*/
resolvedData: ListTypeInfo['prisma']['create'];
resolvedData: ListTypeInfo['prisma']['update'];
addValidationError: (error: string) => void;
};
delete: {
Expand Down Expand Up @@ -227,11 +227,11 @@ type ValidateFieldHook<
/**
* The GraphQL input **before** default values are applied
*/
inputData: ListTypeInfo['inputs']['create'];
inputData: ListTypeInfo['inputs']['update'];
/**
* The GraphQL input **after** being resolved by the field type's input resolver
*/
resolvedData: ListTypeInfo['prisma']['create'];
resolvedData: ListTypeInfo['prisma']['update'];
addValidationError: (error: string) => void;
};
delete: {
Expand Down

0 comments on commit 52337e3

Please sign in to comment.