fix: field appending on duplicate should ignore non string values (#11621)

### What?
When duplicating a document with `unique` fields, we append `- Copy` to
the field value.
The issue is that this is happening when the field is empty resulting in
values being added that look like: `undefined - Copy` or `null - Copy`.

### Why?
We are not checking the incoming value in all cases.

### How?
Checks the value exists, is a string, and is not just an empty space
before appending `- Copy`.

At first glance it looks incorrect to return required fields with
`undefined` - however when duplicating a document, the new document is
always created as a `draft` so it is not an issue to return `undefined`.

Closes #11373
This commit is contained in:
Jessica Chowdhury
2025-03-18 16:01:08 +00:00
committed by GitHub
parent 4a712e1d2c
commit 74996fd511

View File

@@ -2,12 +2,10 @@
// default beforeDuplicate hook for required and unique fields // default beforeDuplicate hook for required and unique fields
import { type FieldAffectingData, type FieldHook, fieldShouldBeLocalized } from './config/types.js' import { type FieldAffectingData, type FieldHook, fieldShouldBeLocalized } from './config/types.js'
const unique: FieldHook = ({ value }) => (typeof value === 'string' ? `${value} - Copy` : undefined) const isStringValue = (value) => typeof value === 'string' && value.trim() !== ''
const unique: FieldHook = ({ value }) => (isStringValue(value) ? `${value} - Copy` : undefined)
const localizedUnique: FieldHook = ({ req, value }) => const localizedUnique: FieldHook = ({ req, value }) =>
value ? `${value} - ${req?.t('general:copy') ?? 'Copy'}` : undefined isStringValue(value) ? `${value} - ${req?.t('general:copy') ?? 'Copy'}` : undefined
const uniqueRequired: FieldHook = ({ value }) => `${value} - Copy`
const localizedUniqueRequired: FieldHook = ({ req, value }) =>
`${value} - ${req?.t('general:copy') ?? 'Copy'}`
export const setDefaultBeforeDuplicate = ( export const setDefaultBeforeDuplicate = (
field: FieldAffectingData, field: FieldAffectingData,
@@ -18,16 +16,14 @@ export const setDefaultBeforeDuplicate = (
(!field.hooks?.beforeDuplicate || (!field.hooks?.beforeDuplicate ||
(Array.isArray(field.hooks.beforeDuplicate) && field.hooks.beforeDuplicate.length === 0)) (Array.isArray(field.hooks.beforeDuplicate) && field.hooks.beforeDuplicate.length === 0))
) { ) {
if ((field.type === 'text' || field.type === 'textarea') && field.required && field.unique) { if (field.unique) {
field.hooks.beforeDuplicate = [ if (['email', 'number', 'point', 'relationship', 'select', 'upload'].includes(field.type)) {
fieldShouldBeLocalized({ field, parentIsLocalized }) field.hooks.beforeDuplicate = [() => undefined]
? localizedUniqueRequired } else if (['code', 'json', 'text', 'textarea'].includes(field.type)) {
: uniqueRequired, field.hooks.beforeDuplicate = fieldShouldBeLocalized({ field, parentIsLocalized })
] ? [localizedUnique]
} else if (field.unique) { : [unique]
field.hooks.beforeDuplicate = [ }
fieldShouldBeLocalized({ field, parentIsLocalized }) ? localizedUnique : unique,
]
} }
} }
} }