#Localization not supported on arrays in versioned collections?

5 messages · Page 1 of 1 (latest)

smoky basin
#

My collection test-collection with versions: { drafts: true } has an array field with localized: true.

I'm able to create docs to the db-collection contents in my mongoDb, however it throws a cast to embedded failed-error on post (see below). Seems it's not able to post the doc to the db-collection _contents_versions.

On patch I'm getting the same error. The draft updates with correct data though.

I'm on 3.60.0.

wispy pastureBOT
smoky basin
#
"message": "_contents_versions validation failed: version: Cast to Embedded failed for value \"{\n  createdAt: '2025-10-22T19:59:05.596Z',\n  updatedAt: '2025-10-22T19:59:05.596Z',\n  name: 'Site Owner Identity',\n  schema: new ObjectId('68f937894098e3ecf7a61627'),\n  fields: { en: [ [Object], [Object], [Object] ] },\n  sites: [ '68a4e2ed1be003f5b16798b5' ],\n  _status: 'draft',\n  id: '68f937894098e3ecf7a61652'\n}\" (type Object) at path \"version\" because of \"TypeError\"",
      "stack":
          ValidationError: _contents_versions validation failed: version: Cast to Embedded failed for value "{
            createdAt: '2025-10-22T19:59:05.596Z',
            updatedAt: '2025-10-22T19:59:05.596Z',
            name: 'Site Owner Identity',
            schema: new ObjectId('68f937894098e3ecf7a61627'),
            fields: { en: [ [Object], [Object], [Object] ] },
            sites: [ '68a4e2ed1be003f5b16798b5' ],
            _status: 'draft',
            id: '68f937894098e3ecf7a61652'
          }" (type Object) at path "version" because of "TypeError"
              at Document.invalidate (/home/ingrid/repos/bestand-app/node_modules/.pnpm/mongoose@8.15.1_@aws-sdk+credential-providers@3.901.0/node_modules/mongoose/lib/document.js:3343:32)
...
smoky basin
#

Bug:
Versions collection fails with "Cast to Embedded failed" when using localized arrays containing blocks (MongoDB)

Environment:

  • Payload: 3.61.1 (persists from 3.60.0)
  • Database: MongoDB

Issue:
Collections with versions: { drafts: true } fail to save versions when localized arrays contain blocks fields. Main collection saves successfully, but version creation throws MongoDB validation errors.

Error:
_contents_versions validation failed: version: Cast to Embedded failed for value "{...}" (type Object) at path "version" because of "TypeError"

Reproduction:

  1. Enable versions with drafts on a collection
  2. Create localized array field containing blocks:
  {
    name: 'fields',
    type: 'array',
    localized: true,
    fields: [
      {
        name: 'value',
        type: 'blocks',
        blocks: [Text, Email, Relationship] // Any blocks
      }
    ]
  }
  1. Create document - main collection saves fine, versions collection fails

Root Cause Analysis:

The issue occurs in the version creation pipeline due to block reference loss during field flattening:

  1. Field Context Loss: In buildVersionCollectionFields() (packages/payload/src/versions/buildCollectionFields.ts:22-25), the version field structure is created using: fields: collection.fields.filter((field) => !('name' in field) || field.name !== 'id')
  2. This filtering process creates new field references that lose the original block definitions.
  3. Block Resolution Failure: During version processing, stripFields() in packages/db-mongodb/src/utilities/transform.ts:369 attempts: maybeBlock = field.blocks.find((each) => each.slug === data.blockType)
  4. But field.blocks is now empty/undefined because the version field structure doesn't preserve the original blocks array.
  5. Data Corruption: Failed block lookup causes fieldData[i] = null (line 375), corrupting the document structure and triggering MongoDB's "Cast to Embedded failed" validation error.
smoky basin
#

Specific Problem:
The version field building process doesn't properly preserve block references when creating flattened field structures, causing block resolution to fail specifically for localized arrays containing blocks.

Workaround:
Remove blocks from localized arrays or disable localization on arrays containing blocks.

Related Issues:

  • #13804 (claimed to fix "localized arrays inside blocks with versions" in 3.55.0) - incomplete fix
  • #11245 (similar MongoDB localized array issues)

Expected:
Versions should preserve block references and save the same data structure as main collection without field context loss.