Skip to content

Commit

Permalink
Handle both old and new validate syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
acburdine committed Aug 13, 2024
1 parent 3e8e356 commit 8ea026e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/long-bees-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@keystone-6/core": patch
---

Handle combined use of old and new validate syntax
43 changes: 17 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,23 @@ 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

const isFnValidate = typeof validate === 'function'

return {
create: merge(validateInput, isFnValidate ? validate : validate?.create),
update: merge(validateInput, isFnValidate ? validate : validate?.update),
delete: merge(validateDelete, isFnValidate ? validate : validate?.delete),
}
}

export function mergeFieldHooks <ListTypeInfo extends BaseListTypeInfo> (
builtin?: InternalFieldHooks<ListTypeInfo>,
hooks?: FieldHooks<ListTypeInfo>,
Expand Down

0 comments on commit 8ea026e

Please sign in to comment.