Skip to content

Commit

Permalink
fix: cannot use empty string in defaultValue on text-like fields (#6842)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
franciscolourenco authored Jun 19, 2024
1 parent 025306f commit 3364385
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/payload/src/fields/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() }),
Expand Down Expand Up @@ -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(),
})
Expand All @@ -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(),
})
Expand All @@ -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({
Expand Down
10 changes: 10 additions & 0 deletions test/fields/collections/Text/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ const TextFields: CollectionConfig = {
},
type: 'text',
},
{
name: 'defaultString',
defaultValue: defaultText,
type: 'text',
},
{
name: 'defaultEmptyString',
defaultValue: '',
type: 'text',
},
{
name: 'defaultFunction',
defaultValue: () => defaultText,
Expand Down
5 changes: 5 additions & 0 deletions test/fields/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 3364385

Please sign in to comment.