diff --git a/.changeset/text-null-default.md b/.changeset/text-null-default.md new file mode 100644 index 00000000000..c77c6bceca1 --- /dev/null +++ b/.changeset/text-null-default.md @@ -0,0 +1,5 @@ +---- +'@keystone-6/core': patch +---- + +Fixes the `text` field type to accept a `defaultValue` of `null` diff --git a/packages/core/src/fields/types/text/index.ts b/packages/core/src/fields/types/text/index.ts index 1c27a5a4a8e..8b5622a607e 100644 --- a/packages/core/src/fields/types/text/index.ts +++ b/packages/core/src/fields/types/text/index.ts @@ -27,7 +27,7 @@ export type TextFieldConfig = match?: { regex: RegExp, explanation?: string } length?: { min?: number, max?: number } } - defaultValue?: string + defaultValue?: string | null db?: { isNullable?: boolean map?: string @@ -94,18 +94,18 @@ export function text ( // 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 defaultValue = isNullable === false || defaultValue_ !== undefined ? defaultValue_ || '' : undefined - const hasValidation = resolveHasValidation(config) + const hasValidation = resolveHasValidation(config) || !isNullable // we make an exception for Text 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, @@ -124,9 +124,7 @@ export function 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) {