From 336438506ce5e54065f59a0352531e0255f5313d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Francisco=20Louren=C3=A7o?= <208149+franciscolourenco@users.noreply.github.com> Date: Wed, 19 Jun 2024 15:35:23 +0200 Subject: [PATCH] fix: cannot use empty string in defaultValue on text-like fields (#6842) ## Description This changes allows empty strings (`''`) to be used 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`. - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change - [x] New feature (non-breaking change which adds functionality) ## Checklist: - [x] I have added tests that prove my fix is effective or that my feature works - [x] Existing test suite passes locally with my changes --- packages/payload/src/fields/config/schema.ts | 8 ++++---- test/fields/collections/Text/index.ts | 10 ++++++++++ test/fields/int.spec.ts | 5 +++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/payload/src/fields/config/schema.ts b/packages/payload/src/fields/config/schema.ts index 3dfe599dd0a..c3be61a1745 100644 --- a/packages/payload/src/fields/config/schema.ts +++ b/packages/payload/src/fields/config/schema.ts @@ -85,7 +85,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() }), @@ -141,7 +141,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(), }) @@ -159,7 +159,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(), }) @@ -175,7 +175,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 e36043c1761..cf9d14b0051 100644 --- a/test/fields/collections/Text/index.ts +++ b/test/fields/collections/Text/index.ts @@ -40,6 +40,16 @@ const TextFields: CollectionConfig = { }, type: 'text', }, + { + name: 'defaultString', + defaultValue: defaultText, + type: 'text', + }, + { + name: 'defaultEmptyString', + defaultValue: '', + type: 'text', + }, { name: 'defaultFunction', defaultValue: () => defaultText, diff --git a/test/fields/int.spec.ts b/test/fields/int.spec.ts index d7aaa9ab317..c3854d64173 100644 --- a/test/fields/int.spec.ts +++ b/test/fields/int.spec.ts @@ -82,10 +82,15 @@ describe('Fields', () => { it('creates with default values', () => { expect(doc.text).toEqual(text) + expect(doc.defaultString).toEqual(defaultText) expect(doc.defaultFunction).toEqual(defaultText) expect(doc.defaultAsync).toEqual(defaultText) }) + it('supports empty strings as default value', () => { + expect(doc.defaultEmptyString).toEqual('') + }) + it('should populate default values in beforeValidate hook', async () => { const { dependentOnFieldWithDefaultValue, fieldWithDefaultValue } = await payload.create({ collection: 'text-fields',