Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions packages/payload/src/fields/config/sanitize.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { deepMergeSimple } from '@payloadcms/translations/utilities'
import { v4 as uuid } from 'uuid'

import type {
CollectionConfig,
Expand All @@ -21,12 +20,12 @@ import {
import { ReservedFieldName } from '../../errors/ReservedFieldName.js'
import { flattenAllFields } from '../../utilities/flattenAllFields.js'
import { formatLabels, toWords } from '../../utilities/formatLabels.js'
import { getFieldByPath } from '../../utilities/getFieldByPath.js'
import { validateTimezones } from '../../utilities/validateTimezones.js'
import { baseBlockFields } from '../baseFields/baseBlockFields.js'
import { baseIDField } from '../baseFields/baseIDField.js'
import { baseTimezoneField } from '../baseFields/timezone/baseField.js'
import { defaultTimezones } from '../baseFields/timezone/defaultTimezones.js'
import { getFieldPaths } from '../getFieldPaths.js'
import { setDefaultBeforeDuplicate } from '../setDefaultBeforeDuplicate.js'
import { validations } from '../validations.js'
import {
Expand Down Expand Up @@ -58,7 +57,16 @@ type Args = {
* When not passed in, assume that join are not supported (globals, arrays, blocks)
*/
joins?: SanitizedJoins
/**
* A string of '-' separated indexes representing where
* to find this field in a given field schema array.
*/
parentIndexPath?: string
parentIsLocalized: boolean
/**
* Path for parent fields relative to their position in the schema.
*/
parentSchemaPath?: string
polymorphicJoins?: SanitizedJoin[]
/**
* If true, a richText field will require an editor property to be set, as the sanitizeFields function will not add it from the payload config if not present.
Expand Down Expand Up @@ -87,7 +95,9 @@ export const sanitizeFields = async ({
isTopLevelField = true,
joinPath = '',
joins,
parentIndexPath = '',
parentIsLocalized,
parentSchemaPath = '',
polymorphicJoins,
requireFieldLevelRichTextEditor = false,
richTextSanitizationPromises,
Expand All @@ -114,6 +124,13 @@ export const sanitizeFields = async ({

const fieldAffectsData = _fieldAffectsData(field)

const { indexPath, schemaPath } = getFieldPaths({
field,
index: i,
parentIndexPath,
parentSchemaPath,
})

if (isTopLevelField && fieldAffectsData && field.name) {
if (collectionConfig && collectionConfig.upload) {
if (reservedBaseUploadFieldNames.includes(field.name)) {
Expand Down Expand Up @@ -336,13 +353,16 @@ export const sanitizeFields = async ({
block._sanitized = true
block.fields = block.fields.concat(baseBlockFields)
block.labels = !block.labels ? formatLabels(block.slug) : block.labels

block.fields = await sanitizeFields({
collectionConfig,
config,
existingFieldNames: new Set(),
fields: block.fields,
isTopLevelField: false,
parentIndexPath: '',
parentIsLocalized: (parentIsLocalized || field.localized)!,
parentSchemaPath: schemaPath + '.' + block.slug,
requireFieldLevelRichTextEditor,
richTextSanitizationPromises,
validRelationships,
Expand All @@ -359,7 +379,9 @@ export const sanitizeFields = async ({
isTopLevelField: isTopLevelField && !fieldAffectsData,
joinPath: fieldAffectsData ? `${joinPath ? joinPath + '.' : ''}${field.name}` : joinPath,
joins,
parentIndexPath: fieldAffectsData ? '' : indexPath,
parentIsLocalized: parentIsLocalized || fieldIsLocalized(field),
parentSchemaPath: schemaPath,
polymorphicJoins,
requireFieldLevelRichTextEditor,
richTextSanitizationPromises,
Expand All @@ -377,14 +399,20 @@ export const sanitizeFields = async ({
tab.label = toWords(tab.name)
}

const { indexPath: tabIndexPath, schemaPath: tabSchemaPath } = getFieldPaths({
field: tab,
index: j,
parentIndexPath: indexPath,
parentSchemaPath: schemaPath,
})

if (
'admin' in tab &&
tab.admin?.condition &&
typeof tab.admin.condition === 'function' &&
!tab.id
) {
// Always attach a UUID to tabs with a condition so there's no conflicts even if there are duplicate nested names
tab.id = isNamedTab ? `${tab.name}_${uuid()}` : uuid()
tab.id = tabSchemaPath
}

tab.fields = await sanitizeFields({
Expand All @@ -395,7 +423,9 @@ export const sanitizeFields = async ({
isTopLevelField: isTopLevelField && !isNamedTab,
joinPath: isNamedTab ? `${joinPath ? joinPath + '.' : ''}${tab.name}` : joinPath,
joins,
parentIndexPath: isNamedTab ? '' : tabIndexPath,
parentIsLocalized: parentIsLocalized || (isNamedTab && tab.localized)!,
parentSchemaPath: tabSchemaPath,
polymorphicJoins,
requireFieldLevelRichTextEditor,
richTextSanitizationPromises,
Expand Down
1 change: 1 addition & 0 deletions packages/payload/src/fields/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,7 @@ type TabBase = {
*/
description?: LabelFunction | StaticDescription
fields: Field[]
// TODO: Deprecate this in favor of a schemaPath property on every field
id?: string
interfaceName?: string
saveToJWT?: boolean | string
Expand Down
Loading