fix(db-mongodb): strip deleted from the config blocks from the result (#12869)

If you (using the MongoDB adapter) delete a block from the payload
config, but still have some data with that block in the DB, you'd
receive in the admin panel an error like:
```
Block with type "cta" was found in block data, but no block with that type is defined in the config for field with schema path pages.blocks
```

Now, we remove those "unknown" blocks at the DB adapter level.

Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
This commit is contained in:
Sasha
2025-06-27 12:30:48 +03:00
committed by GitHub
parent 3830d710a4
commit 54afaf9529
7 changed files with 96 additions and 19 deletions

View File

@@ -277,7 +277,9 @@ const stripFields = ({
continue
}
for (const data of localeData) {
let hasNull = false
for (let i = 0; i < localeData.length; i++) {
const data = localeData[i]
let fields: FlattenedField[] | null = null
if (field.type === 'array') {
@@ -286,11 +288,17 @@ const stripFields = ({
let maybeBlock: FlattenedBlock | undefined = undefined
if (field.blockReferences) {
const maybeBlockReference = field.blockReferences.find(
(each) => typeof each === 'object' && each.slug === data.blockType,
)
if (maybeBlockReference && typeof maybeBlockReference === 'object') {
maybeBlock = maybeBlockReference
const maybeBlockReference = field.blockReferences.find((each) => {
const slug = typeof each === 'string' ? each : each.slug
return slug === data.blockType
})
if (maybeBlockReference) {
if (typeof maybeBlockReference === 'object') {
maybeBlock = maybeBlockReference
} else {
maybeBlock = config.blocks?.find((each) => each.slug === maybeBlockReference)
}
}
}
@@ -300,6 +308,9 @@ const stripFields = ({
if (maybeBlock) {
fields = maybeBlock.flattenedFields
} else {
localeData[i] = null
hasNull = true
}
}
@@ -310,6 +321,10 @@ const stripFields = ({
stripFields({ config, data, fields, reservedKeys })
}
if (hasNull) {
fieldData[localeKey] = localeData.filter(Boolean)
}
continue
} else {
stripFields({ config, data: localeData, fields: field.flattenedFields, reservedKeys })
@@ -323,7 +338,10 @@ const stripFields = ({
continue
}
for (const data of fieldData) {
let hasNull = false
for (let i = 0; i < fieldData.length; i++) {
const data = fieldData[i]
let fields: FlattenedField[] | null = null
if (field.type === 'array') {
@@ -332,12 +350,17 @@ const stripFields = ({
let maybeBlock: FlattenedBlock | undefined = undefined
if (field.blockReferences) {
const maybeBlockReference = field.blockReferences.find(
(each) => typeof each === 'object' && each.slug === data.blockType,
)
const maybeBlockReference = field.blockReferences.find((each) => {
const slug = typeof each === 'string' ? each : each.slug
return slug === data.blockType
})
if (maybeBlockReference && typeof maybeBlockReference === 'object') {
maybeBlock = maybeBlockReference
if (maybeBlockReference) {
if (typeof maybeBlockReference === 'object') {
maybeBlock = maybeBlockReference
} else {
maybeBlock = config.blocks?.find((each) => each.slug === maybeBlockReference)
}
}
}
@@ -347,6 +370,9 @@ const stripFields = ({
if (maybeBlock) {
fields = maybeBlock.flattenedFields
} else {
fieldData[i] = null
hasNull = true
}
}
@@ -357,6 +383,10 @@ const stripFields = ({
stripFields({ config, data, fields, reservedKeys })
}
if (hasNull) {
data[field.name] = fieldData.filter(Boolean)
}
continue
} else {
stripFields({ config, data: fieldData, fields: field.flattenedFields, reservedKeys })