Skip to content
Open
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
10 changes: 9 additions & 1 deletion packages/payload/src/fields/hooks/beforeChange/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ export const promise = async ({
? fieldLabelPath
: buildFieldLabel(
fieldLabelPath,
`${getTranslatedLabel(field?.label || field?.name, req.i18n)} > ${req.t('fields:block')} ${rowIndex + 1} (${getTranslatedLabel(block?.labels?.singular || blockTypeToMatch, req.i18n)})`,
`${getTranslatedLabel(field?.label || field?.name, req.i18n)} > ${req.t('fields:block')} ${rowIndex + 1} (${getTranslatedLabel(block?.labels?.singular || blockTypeToMatch || 'unknown', req.i18n)})`,
)

if (block) {
Expand Down Expand Up @@ -399,6 +399,14 @@ export const promise = async ({
skipValidation: skipValidationFromHere,
}),
)
} else if (!skipValidationFromHere) {
errors.push({
label: blockLabelPath,
message: req.t('validation:invalidBlock', {
block: blockTypeToMatch || 'unknown',
}),
path: `${path}.${rowIndex}.id`,
})
}
})

Expand Down
32 changes: 32 additions & 0 deletions test/fields/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3124,6 +3124,38 @@ describe('Fields', () => {
)
})

it('should reject blocks without a blockType', async () => {
await expect(
payload.create({
collection: blockFieldsSlug,
data: {
blocks: [
{
blockName: 'content',
text: 'Will not be saved',
},
],
} as any,
}),
).rejects.toThrow('The following field is invalid: Blocks > Block 1 (unknown)')
})

it('should reject blocks with an unknown blockType', async () => {
await expect(
payload.create({
collection: blockFieldsSlug,
data: {
blocks: [
{
blockType: 'unknownBlock',
text: 'Will not be saved',
},
],
} as any,
}),
).rejects.toThrow('The following field is invalid: Blocks > Block 1 (unknownBlock)')
})

// TODO: re-enable on sqlite once the drizzle sqlite adapter's createJSONQuery supports
// lexical's `{root: {children: [...]}}` shape
it(
Expand Down
Loading