Skip to content

Commit 6bf7d86

Browse files
committed
fix text defaultValue not accepting null
1 parent e9f9956 commit 6bf7d86

File tree

1 file changed

+7
-5
lines changed
  • packages/core/src/fields/types/text

1 file changed

+7
-5
lines changed

packages/core/src/fields/types/text/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export type TextFieldConfig<ListTypeInfo extends BaseListTypeInfo> =
2727
match?: { regex: RegExp, explanation?: string }
2828
length?: { min?: number, max?: number }
2929
}
30-
defaultValue?: string
30+
defaultValue?: string | null
3131
db?: {
3232
isNullable?: boolean
3333
map?: string
@@ -94,18 +94,20 @@ export function text <ListTypeInfo extends BaseListTypeInfo>(
9494

9595
// defaulted to false as a zero length string is preferred to null
9696
const isNullable = config.db?.isNullable ?? false
97-
const fieldLabel = config.label ?? humanize(meta.fieldKey)
97+
if (defaultValue_ === null && !isNullable) throw new Error(`The text field at ${meta.listKey}.${meta.fieldKey} cannot default to null`)
9898

9999
assertReadIsNonNullAllowed(meta, config, isNullable)
100+
101+
const defaultValue = isNullable ? (defaultValue_ ?? null) : (defaultValue_ ?? '')
102+
const fieldLabel = config.label ?? humanize(meta.fieldKey)
100103
const mode = isNullable ? 'optional' : 'required'
101-
const defaultValue = isNullable === false || defaultValue_ !== undefined ? defaultValue_ || '' : undefined
102-
const hasValidation = resolveHasValidation(config)
104+
const hasValidation = resolveHasValidation(config) || !isNullable // we make an exception for Text
103105

104106
return fieldType({
105107
kind: 'scalar',
106108
mode,
107109
scalar: 'String',
108-
default: defaultValue === undefined ? undefined : { kind: 'literal', value: defaultValue },
110+
default: defaultValue === null ? undefined : { kind: 'literal', value: defaultValue },
109111
index: isIndexed === true ? 'index' : isIndexed || undefined,
110112
map: config.db?.map,
111113
nativeType: config.db?.nativeType,

0 commit comments

Comments
 (0)