From 317bc070e4864b7070c26bb2863963b635a5966b Mon Sep 17 00:00:00 2001 From: Dan Ribbens Date: Wed, 19 Jun 2024 10:02:31 -0400 Subject: [PATCH] fix: cannot use empty strings defaultValue in text-like fields (#6847) Copy of https://github.com/payloadcms/payload/pull/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. --- packages/payload/src/fields/config/schema.ts | 8 ++++---- test/fields/collections/Text/index.ts | 12 +++++++++++- test/fields/int.spec.ts | 16 +++++++++------- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/packages/payload/src/fields/config/schema.ts b/packages/payload/src/fields/config/schema.ts index d9200e095..592922de4 100644 --- a/packages/payload/src/fields/config/schema.ts +++ b/packages/payload/src/fields/config/schema.ts @@ -93,7 +93,7 @@ export const text = baseField.keys({ .try(joi.object().pattern(joi.string(), [joi.string()]), joi.string()), rtl: joi.boolean(), }), - defaultValue: joi.alternatives().try(joi.string(), joi.func()), + defaultValue: joi.alternatives().try(joi.string().allow(''), joi.func()), hasMany: joi.boolean().default(false), maxLength: joi.number(), maxRows: joi.number().when('hasMany', { is: joi.not(true), then: joi.forbidden() }), @@ -149,7 +149,7 @@ export const textarea = baseField.keys({ rows: joi.number(), rtl: joi.boolean(), }), - defaultValue: joi.alternatives().try(joi.string(), joi.func()), + defaultValue: joi.alternatives().try(joi.string().allow(''), joi.func()), maxLength: joi.number(), minLength: joi.number(), }) @@ -167,7 +167,7 @@ export const email = baseField.keys({ }), placeholder: joi.string(), }), - defaultValue: joi.alternatives().try(joi.string(), joi.func()), + defaultValue: joi.alternatives().try(joi.string().allow(''), joi.func()), maxLength: joi.number(), minLength: joi.number(), }) @@ -183,7 +183,7 @@ export const code = baseField.keys({ editorOptions: joi.object().unknown(), // Editor['options'] @monaco-editor/react language: joi.string(), }), - defaultValue: joi.alternatives().try(joi.string(), joi.func()), + defaultValue: joi.alternatives().try(joi.string().allow(''), joi.func()), }) export const json = baseField.keys({ diff --git a/test/fields/collections/Text/index.ts b/test/fields/collections/Text/index.ts index a0e386731..1c7c54716 100644 --- a/test/fields/collections/Text/index.ts +++ b/test/fields/collections/Text/index.ts @@ -41,6 +41,16 @@ const TextFields: CollectionConfig = { es: 'Text es', }, }, + { + name: 'defaultString', + type: 'text', + defaultValue: defaultText, + }, + { + name: 'defaultEmptyString', + type: 'text', + defaultValue: '', + }, { name: 'defaultFunction', type: 'text', @@ -48,6 +58,7 @@ const TextFields: CollectionConfig = { }, { name: 'defaultAsync', + type: 'text', defaultValue: async (): Promise => { return new Promise((resolve) => setTimeout(() => { @@ -55,7 +66,6 @@ const TextFields: CollectionConfig = { }, 1), ) }, - type: 'text', }, { name: 'overrideLength', diff --git a/test/fields/int.spec.ts b/test/fields/int.spec.ts index c0ef50217..8a81f8260 100644 --- a/test/fields/int.spec.ts +++ b/test/fields/int.spec.ts @@ -79,9 +79,11 @@ describe('Fields', () => { }) it('creates with default values', () => { - expect(doc.text).toEqual(text) - expect(doc.defaultFunction).toEqual(defaultText) - expect(doc.defaultAsync).toEqual(defaultText) + expect(doc.text).toStrictEqual(text) + expect(doc.defaultString).toStrictEqual(defaultText) + expect(doc.defaultEmptyString).toStrictEqual('') + expect(doc.defaultFunction).toStrictEqual(defaultText) + expect(doc.defaultAsync).toStrictEqual(defaultText) }) it('should populate default values in beforeValidate hook', async () => { @@ -118,16 +120,16 @@ describe('Fields', () => { const hit = await payload.create({ collection: 'text-fields', data: { - text: 'required', hasMany: ['one', 'five'], + text: 'required', }, }) const miss = await payload.create({ collection: 'text-fields', data: { - text: 'required', hasMany: ['two'], + text: 'required', }, }) @@ -950,15 +952,15 @@ describe('Fields', () => { it('should hot module reload and still be able to create', async () => { const testDoc1 = await payload.findByID({ - collection: tabsFieldsSlug, id: document.id, + collection: tabsFieldsSlug, }) await reload(payload.config, payload) const testDoc2 = await payload.findByID({ - collection: tabsFieldsSlug, id: document.id, + collection: tabsFieldsSlug, }) expect(testDoc1.id).toStrictEqual(testDoc2.id)