Skip to content

Commit

Permalink
Fix validation hook merge function passthrough (#9278)
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 committed Aug 20, 2024
1 parent 40e85b7 commit dc3802a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/fix-hooks-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@keystone-6/core": patch
---

Fix blended usage of `{field}.hooks.validateInput` and `{field}.validate.*` resulting in an error
44 changes: 18 additions & 26 deletions packages/core/src/fields/resolve-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,6 @@ import {
export type InternalFieldHooks<ListTypeInfo extends BaseListTypeInfo> =
Omit<FieldHooks<ListTypeInfo>, 'validateInput' | 'validateDelete' | 'resolveInput'>

/** @deprecated, TODO: remove in breaking change */
function resolveValidateHooks <ListTypeInfo extends BaseListTypeInfo> ({
validate,
validateInput,
validateDelete
}: FieldHooks<ListTypeInfo>): Exclude<FieldHooks<ListTypeInfo>["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<void>,
Expand All @@ -47,6 +21,20 @@ function merge <
}
}

/** @deprecated, TODO: remove in breaking change */
function resolveValidateHooks <ListTypeInfo extends BaseListTypeInfo> ({
validate,
validateInput,
validateDelete
}: FieldHooks<ListTypeInfo>): Exclude<FieldHooks<ListTypeInfo>["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 <ListTypeInfo extends BaseListTypeInfo> (
builtin?: InternalFieldHooks<ListTypeInfo>,
hooks?: FieldHooks<ListTypeInfo>,
Expand All @@ -67,5 +55,9 @@ export function mergeFieldHooks <ListTypeInfo extends BaseListTypeInfo> (
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<ListTypeInfo>
}

0 comments on commit dc3802a

Please sign in to comment.