From dc3802a579306b6e6fa5fd2e65a73142dc5f3e86 Mon Sep 17 00:00:00 2001 From: Austin Burdine Date: Tue, 20 Aug 2024 02:24:54 -0500 Subject: [PATCH] Fix validation hook merge function passthrough (#9278) Co-authored-by: Daniel Cousens <413395+dcousens@users.noreply.github.com> --- .changeset/fix-hooks-errors.md | 5 +++ packages/core/src/fields/resolve-hooks.ts | 44 ++++++++++------------- 2 files changed, 23 insertions(+), 26 deletions(-) create mode 100644 .changeset/fix-hooks-errors.md diff --git a/.changeset/fix-hooks-errors.md b/.changeset/fix-hooks-errors.md new file mode 100644 index 00000000000..7a7b2fa136b --- /dev/null +++ b/.changeset/fix-hooks-errors.md @@ -0,0 +1,5 @@ +--- +"@keystone-6/core": patch +--- + +Fix blended usage of `{field}.hooks.validateInput` and `{field}.validate.*` resulting in an error diff --git a/packages/core/src/fields/resolve-hooks.ts b/packages/core/src/fields/resolve-hooks.ts index 3ab9ede6633..e093cfc081f 100644 --- a/packages/core/src/fields/resolve-hooks.ts +++ b/packages/core/src/fields/resolve-hooks.ts @@ -9,32 +9,6 @@ import { export type InternalFieldHooks = Omit, 'validateInput' | 'validateDelete' | 'resolveInput'> -/** @deprecated, TODO: remove in breaking change */ -function resolveValidateHooks ({ - validate, - validateInput, - validateDelete -}: FieldHooks): Exclude["validate"], Function> | undefined { - if (validateInput || validateDelete) { - return { - create: validateInput, - update: validateInput, - delete: validateDelete, - } - } - - if (!validate) return - if (typeof validate === 'function') { - return { - create: validate, - update: validate, - delete: validate - } - } - - return validate -} - function merge < R, A extends (r: R) => MaybePromise, @@ -47,6 +21,20 @@ function merge < } } +/** @deprecated, TODO: remove in breaking change */ +function resolveValidateHooks ({ + validate, + validateInput, + validateDelete +}: FieldHooks): Exclude["validate"], Function> | undefined { + if (!validate && !validateInput && !validateDelete) return + return { + create: merge(validateInput, typeof validate === 'function' ? validate : validate?.create), + update: merge(validateInput, typeof validate === 'function' ? validate : validate?.update), + delete: merge(validateDelete, typeof validate === 'function' ? validate : validate?.delete), + } +} + export function mergeFieldHooks ( builtin?: InternalFieldHooks, hooks?: FieldHooks, @@ -67,5 +55,9 @@ export function mergeFieldHooks ( update: merge(builtinValidate?.update, hooksValidate?.update), delete: merge(builtinValidate?.delete, hooksValidate?.delete) } : undefined, + + // TODO: remove in breaking change + validateInput: undefined, // prevent continuation + validateDelete: undefined, // prevent continuation } satisfies FieldHooks }