-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Adds {field}.hooks.validate.[create|update|delete] hooks
#9057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a2609ed
rm redundant tests
dcousens d67a0ee
flatten tests
dcousens cb11723
add {field}.hooks.validate
dcousens 8c17903
add some field hook tests for beforeOperation, afterOperation and val…
dcousens 6a0d6ee
add field hook tests for resolveInput
dcousens 22baf8b
different test seeds from inputs from outputs
dcousens ed16751
differentiate create, update, delete operations in field hook tests
dcousens f185cb7
fix text field type
dcousens 2517c74
use for loops for field CRUD tests
dcousens 1c3162b
add new validation hook support for text, bigint and timestamp field …
dcousens 8329260
add defaultValue of null for field type text()
dcousens 26d3772
use TypeError for type errors
dcousens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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': minor | ||
| ---- | ||
|
|
||
| Adds `{field}.hooks.validate.[create|update|delete]` hooks, deprecates `validateInput` and `validateDelete` (throws if incompatible) |
This file contains hidden or 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 | ||
| ---- | ||
|
|
||
| Fixes the `text` field type to accept a `defaultValue` of `null` |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -2,12 +2,15 @@ import { humanize } from '../../../lib/utils' | |
| import { | ||
| type BaseListTypeInfo, | ||
| type CommonFieldConfig, | ||
| type FieldTypeFunc, | ||
| fieldType, | ||
| orderDirectionEnum, | ||
| type FieldTypeFunc, | ||
| } from '../../../types' | ||
| import { graphql } from '../../..' | ||
| import { assertReadIsNonNullAllowed } from '../../non-null-graphql' | ||
| import { | ||
| assertReadIsNonNullAllowed, | ||
| resolveHasValidation, | ||
| } from '../../non-null-graphql' | ||
| import { filters } from '../../filters' | ||
|
|
||
| export type TextFieldConfig<ListTypeInfo extends BaseListTypeInfo> = | ||
|
|
@@ -24,7 +27,7 @@ export type TextFieldConfig<ListTypeInfo extends BaseListTypeInfo> = | |
| match?: { regex: RegExp, explanation?: string } | ||
| length?: { min?: number, max?: number } | ||
| } | ||
| defaultValue?: string | ||
| defaultValue?: string | null | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unlike other fields, the ambiguity of |
||
| db?: { | ||
| isNullable?: boolean | ||
| map?: string | ||
|
|
@@ -53,62 +56,56 @@ export type TextFieldConfig<ListTypeInfo extends BaseListTypeInfo> = | |
| } | ||
| } | ||
|
|
||
| export const text = | ||
| <ListTypeInfo extends BaseListTypeInfo>({ | ||
| export function text <ListTypeInfo extends BaseListTypeInfo>( | ||
| config: TextFieldConfig<ListTypeInfo> = {} | ||
| ): FieldTypeFunc<ListTypeInfo> { | ||
| const { | ||
| isIndexed, | ||
| defaultValue: _defaultValue, | ||
| validation: _validation, | ||
| ...config | ||
| }: TextFieldConfig<ListTypeInfo> = {}): FieldTypeFunc<ListTypeInfo> => | ||
| meta => { | ||
| defaultValue: defaultValue_, | ||
| validation: validation_ | ||
| } = config | ||
|
|
||
| return (meta) => { | ||
| for (const type of ['min', 'max'] as const) { | ||
| const val = _validation?.length?.[type] | ||
| const val = validation_?.length?.[type] | ||
| if (val !== undefined && (!Number.isInteger(val) || val < 0)) { | ||
| throw new Error( | ||
| `The text field at ${meta.listKey}.${meta.fieldKey} specifies validation.length.${type}: ${val} but it must be a positive integer` | ||
| ) | ||
| throw new Error(`The text field at ${meta.listKey}.${meta.fieldKey} specifies validation.length.${type}: ${val} but it must be a positive integer`) | ||
| } | ||
| if (_validation?.isRequired && val !== undefined && val === 0) { | ||
| throw new Error( | ||
| `The text field at ${meta.listKey}.${meta.fieldKey} specifies validation.isRequired: true and validation.length.${type}: 0, this is not allowed because validation.isRequired implies at least a min length of 1` | ||
| ) | ||
| if (validation_?.isRequired && val !== undefined && val === 0) { | ||
| throw new Error(`The text field at ${meta.listKey}.${meta.fieldKey} specifies validation.isRequired: true and validation.length.${type}: 0, this is not allowed because validation.isRequired implies at least a min length of 1`) | ||
| } | ||
| } | ||
|
|
||
| if ( | ||
| _validation?.length?.min !== undefined && | ||
| _validation?.length?.max !== undefined && | ||
| _validation?.length?.min > _validation?.length?.max | ||
| validation_?.length?.min !== undefined && | ||
| validation_?.length?.max !== undefined && | ||
| validation_?.length?.min > validation_?.length?.max | ||
| ) { | ||
| throw new Error( | ||
| `The text field at ${meta.listKey}.${meta.fieldKey} specifies a validation.length.max that is less than the validation.length.min, and therefore has no valid options` | ||
| ) | ||
| throw new Error(`The text field at ${meta.listKey}.${meta.fieldKey} specifies a validation.length.max that is less than the validation.length.min, and therefore has no valid options`) | ||
| } | ||
|
|
||
| const validation = { | ||
| ..._validation, | ||
| const validation = validation_ ? { | ||
| ...validation_, | ||
| length: { | ||
| min: _validation?.isRequired ? _validation?.length?.min ?? 1 : _validation?.length?.min, | ||
| max: _validation?.length?.max, | ||
| min: validation_?.isRequired ? validation_?.length?.min ?? 1 : validation_?.length?.min, | ||
| max: validation_?.length?.max, | ||
| }, | ||
| } | ||
| } : undefined | ||
|
|
||
| // defaulted to false as a zero length string is preferred to null | ||
| const isNullable = config.db?.isNullable ?? false | ||
|
|
||
| const fieldLabel = config.label ?? humanize(meta.fieldKey) | ||
|
|
||
| assertReadIsNonNullAllowed(meta, config, isNullable) | ||
|
|
||
| const defaultValue = isNullable ? (defaultValue_ ?? null) : (defaultValue_ ?? '') | ||
| const fieldLabel = config.label ?? humanize(meta.fieldKey) | ||
| const mode = isNullable ? 'optional' : 'required' | ||
| const hasValidation = resolveHasValidation(config) || !isNullable // we make an exception for Text | ||
|
|
||
| const defaultValue = | ||
| isNullable === false || _defaultValue !== undefined ? _defaultValue || '' : undefined | ||
| return fieldType({ | ||
| kind: 'scalar', | ||
| mode, | ||
| scalar: 'String', | ||
| default: defaultValue === undefined ? undefined : { kind: 'literal', value: defaultValue }, | ||
| default: (defaultValue === null) ? undefined : { kind: 'literal', value: defaultValue }, | ||
| index: isIndexed === true ? 'index' : isIndexed || undefined, | ||
| map: config.db?.map, | ||
| nativeType: config.db?.nativeType, | ||
|
|
@@ -117,7 +114,7 @@ export const text = | |
| ...config, | ||
| hooks: { | ||
| ...config.hooks, | ||
| async validateInput (args) { | ||
| validateInput: hasValidation ? async (args) => { | ||
| const val = args.resolvedData[meta.fieldKey] | ||
| if (val === null && (validation?.isRequired || isNullable === false)) { | ||
| args.addValidationError(`${fieldLabel} is required`) | ||
|
|
@@ -127,25 +124,19 @@ export const text = | |
| if (validation.length.min === 1) { | ||
| args.addValidationError(`${fieldLabel} must not be empty`) | ||
| } else { | ||
| args.addValidationError( | ||
| `${fieldLabel} must be at least ${validation.length.min} characters long` | ||
| ) | ||
| args.addValidationError(`${fieldLabel} must be at least ${validation.length.min} characters long`) | ||
| } | ||
| } | ||
| if (validation?.length?.max !== undefined && val.length > validation.length.max) { | ||
| args.addValidationError( | ||
| `${fieldLabel} must be no longer than ${validation.length.max} characters` | ||
| ) | ||
| args.addValidationError(`${fieldLabel} must be no longer than ${validation.length.max} characters`) | ||
| } | ||
| if (validation?.match && !validation.match.regex.test(val)) { | ||
| args.addValidationError( | ||
| validation.match.explanation || `${fieldLabel} must match ${validation.match.regex}` | ||
| ) | ||
| args.addValidationError(validation.match.explanation || `${fieldLabel} must match ${validation.match.regex}`) | ||
| } | ||
| } | ||
|
|
||
| await config.hooks?.validateInput?.(args) | ||
| }, | ||
| } : config.hooks?.validateInput | ||
| }, | ||
| input: { | ||
| uniqueWhere: | ||
|
|
@@ -199,6 +190,7 @@ export const text = | |
| }, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| export type TextFieldMeta = { | ||
| displayMode: 'input' | 'textarea' | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a type error, and I think we should remove this in the future