forked from keystonejs/keystone
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update built-in fields to new validate hook syntax
closes keystonejs#9165 - this updates the built-in fields using validateInput to use the new unified validation syntax, and handles merging with the old syntax to ensure user-land old syntax still works
- Loading branch information
Showing
15 changed files
with
424 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@keystone-6/core": patch | ||
--- | ||
|
||
Update built-in fields to use newer validate hook syntax |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import type { FieldHooks, BaseListTypeInfo } from '../types' | ||
|
||
function splitValidateHooks <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 undefined | ||
|
||
if (typeof validate === 'function') { | ||
return { create: validate, update: validate, delete: validate } | ||
} | ||
|
||
return validate | ||
} | ||
|
||
// force new syntax for built-in fields | ||
// also, we don't allow built-in hooks to specify resolveInput, | ||
// since they can do it via graphql resolvers | ||
export type InternalFieldHooks<ListTypeInfo extends BaseListTypeInfo> = | ||
Omit<FieldHooks<ListTypeInfo>, 'validateInput' | 'validateDelete' | 'resolveInput'> | ||
|
||
/** | ||
* Utility function to convert deprecated field hook syntax to the new syntax | ||
* Handles merging any built-in field hooks into the user-provided hooks | ||
*/ | ||
export function mergeFieldHooks <ListTypeInfo extends BaseListTypeInfo> ( | ||
builtin?: InternalFieldHooks<ListTypeInfo>, | ||
hooks: FieldHooks<ListTypeInfo> = {}, | ||
): FieldHooks<ListTypeInfo> { | ||
if (builtin === undefined) return hooks | ||
|
||
const result: FieldHooks<ListTypeInfo> = { | ||
resolveInput: hooks?.resolveInput, | ||
} | ||
|
||
if (hooks.beforeOperation || builtin.beforeOperation) { | ||
result.beforeOperation = async (args) => { | ||
await hooks.beforeOperation?.(args) | ||
await builtin.beforeOperation?.(args) | ||
} | ||
} | ||
|
||
if (hooks.afterOperation || builtin.afterOperation) { | ||
result.afterOperation = async (args) => { | ||
await hooks.afterOperation?.(args) | ||
await builtin.afterOperation?.(args) | ||
} | ||
} | ||
|
||
const builtinValidate = splitValidateHooks(builtin) | ||
const fieldValidate = splitValidateHooks(hooks) | ||
|
||
if (builtinValidate || fieldValidate) { | ||
result.validate = { | ||
create: async (args) => { | ||
await builtinValidate?.create?.(args) | ||
await fieldValidate?.create?.(args) | ||
}, | ||
update: async (args) => { | ||
await builtinValidate?.update?.(args) | ||
await fieldValidate?.update?.(args) | ||
}, | ||
delete: async (args) => { | ||
await builtinValidate?.delete?.(args) | ||
await fieldValidate?.delete?.(args) | ||
}, | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.