Skip to content

Commit 317bc07

Browse files
authored
fix: cannot use empty strings defaultValue in text-like fields (#6847)
Copy of #6842 for beta Allows empty strings ('') as defaultValue for fields of types: 'text'; 'textarea'; 'email'; 'code'. This can be useful when you want to ensure the value is always a string instead of null/undefined.
1 parent 5a994d9 commit 317bc07

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

packages/payload/src/fields/config/schema.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const text = baseField.keys({
9393
.try(joi.object().pattern(joi.string(), [joi.string()]), joi.string()),
9494
rtl: joi.boolean(),
9595
}),
96-
defaultValue: joi.alternatives().try(joi.string(), joi.func()),
96+
defaultValue: joi.alternatives().try(joi.string().allow(''), joi.func()),
9797
hasMany: joi.boolean().default(false),
9898
maxLength: joi.number(),
9999
maxRows: joi.number().when('hasMany', { is: joi.not(true), then: joi.forbidden() }),
@@ -149,7 +149,7 @@ export const textarea = baseField.keys({
149149
rows: joi.number(),
150150
rtl: joi.boolean(),
151151
}),
152-
defaultValue: joi.alternatives().try(joi.string(), joi.func()),
152+
defaultValue: joi.alternatives().try(joi.string().allow(''), joi.func()),
153153
maxLength: joi.number(),
154154
minLength: joi.number(),
155155
})
@@ -167,7 +167,7 @@ export const email = baseField.keys({
167167
}),
168168
placeholder: joi.string(),
169169
}),
170-
defaultValue: joi.alternatives().try(joi.string(), joi.func()),
170+
defaultValue: joi.alternatives().try(joi.string().allow(''), joi.func()),
171171
maxLength: joi.number(),
172172
minLength: joi.number(),
173173
})
@@ -183,7 +183,7 @@ export const code = baseField.keys({
183183
editorOptions: joi.object().unknown(), // Editor['options'] @monaco-editor/react
184184
language: joi.string(),
185185
}),
186-
defaultValue: joi.alternatives().try(joi.string(), joi.func()),
186+
defaultValue: joi.alternatives().try(joi.string().allow(''), joi.func()),
187187
})
188188

189189
export const json = baseField.keys({

test/fields/collections/Text/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,31 @@ const TextFields: CollectionConfig = {
4141
es: 'Text es',
4242
},
4343
},
44+
{
45+
name: 'defaultString',
46+
type: 'text',
47+
defaultValue: defaultText,
48+
},
49+
{
50+
name: 'defaultEmptyString',
51+
type: 'text',
52+
defaultValue: '',
53+
},
4454
{
4555
name: 'defaultFunction',
4656
type: 'text',
4757
defaultValue: () => defaultText,
4858
},
4959
{
5060
name: 'defaultAsync',
61+
type: 'text',
5162
defaultValue: async (): Promise<string> => {
5263
return new Promise((resolve) =>
5364
setTimeout(() => {
5465
resolve(defaultText)
5566
}, 1),
5667
)
5768
},
58-
type: 'text',
5969
},
6070
{
6171
name: 'overrideLength',

test/fields/int.spec.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@ describe('Fields', () => {
7979
})
8080

8181
it('creates with default values', () => {
82-
expect(doc.text).toEqual(text)
83-
expect(doc.defaultFunction).toEqual(defaultText)
84-
expect(doc.defaultAsync).toEqual(defaultText)
82+
expect(doc.text).toStrictEqual(text)
83+
expect(doc.defaultString).toStrictEqual(defaultText)
84+
expect(doc.defaultEmptyString).toStrictEqual('')
85+
expect(doc.defaultFunction).toStrictEqual(defaultText)
86+
expect(doc.defaultAsync).toStrictEqual(defaultText)
8587
})
8688

8789
it('should populate default values in beforeValidate hook', async () => {
@@ -118,16 +120,16 @@ describe('Fields', () => {
118120
const hit = await payload.create({
119121
collection: 'text-fields',
120122
data: {
121-
text: 'required',
122123
hasMany: ['one', 'five'],
124+
text: 'required',
123125
},
124126
})
125127

126128
const miss = await payload.create({
127129
collection: 'text-fields',
128130
data: {
129-
text: 'required',
130131
hasMany: ['two'],
132+
text: 'required',
131133
},
132134
})
133135

@@ -950,15 +952,15 @@ describe('Fields', () => {
950952

951953
it('should hot module reload and still be able to create', async () => {
952954
const testDoc1 = await payload.findByID({
953-
collection: tabsFieldsSlug,
954955
id: document.id,
956+
collection: tabsFieldsSlug,
955957
})
956958

957959
await reload(payload.config, payload)
958960

959961
const testDoc2 = await payload.findByID({
960-
collection: tabsFieldsSlug,
961962
id: document.id,
963+
collection: tabsFieldsSlug,
962964
})
963965

964966
expect(testDoc1.id).toStrictEqual(testDoc2.id)

0 commit comments

Comments
 (0)