Skip to content

Commit

Permalink
fix(db-mongodb): potential errors in sanitizeRelationshipIDs with `…
Browse files Browse the repository at this point in the history
…ref` being a non object (#9292)

### What?
Fixes potential errors when passed to `sanitizeRelationships` `ref`
could potentially be a non object (for example `string`) because of
having in the database data in old structure.
```
"Cannot create property 'a' on string 'B'",
``` 

### Why?
Necessary particularly for the migration script, as it migrates
everything including versions that can have outdated data.

### How?
Ensures passed `ref` is an `object`.
  • Loading branch information
r1tsuu authored Nov 18, 2024
1 parent 488c28c commit 665b353
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions packages/db-mongodb/src/utilities/sanitizeRelationshipIDs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,26 @@ export const sanitizeRelationshipIDs = ({
fields,
}: Args): Record<string, unknown> => {
const sanitize: TraverseFieldsCallback = ({ field, ref }) => {
if (!ref || typeof ref !== 'object') {
return
}

if (field.type === 'relationship' || field.type === 'upload') {
if (!ref[field.name]) {
return
}

// handle localized relationships
if (config.localization && field.localized) {
const locales = config.localization.locales
const fieldRef = ref[field.name]
if (typeof fieldRef !== 'object') {
return
}

for (const { code } of locales) {
if (ref[field.name]?.[code]) {
const value = ref[field.name][code]
const value = ref[field.name][code]
if (value) {
sanitizeRelationship({ config, field, locale: code, ref: fieldRef, value })
}
}
Expand Down

0 comments on commit 665b353

Please sign in to comment.