diff --git a/packages/core/database/src/__tests__/update-guard.test.ts b/packages/core/database/src/__tests__/update-guard.test.ts index 206a2d6bf..acc15a1f6 100644 --- a/packages/core/database/src/__tests__/update-guard.test.ts +++ b/packages/core/database/src/__tests__/update-guard.test.ts @@ -108,6 +108,19 @@ describe('update-guard', () => { }); }); + test('association with null array', () => { + const values = { + name: 'u1', + posts: [null], + }; + + const guard = new UpdateGuard(); + guard.setModel(User.model); + const sanitized = guard.sanitize(values); + + expect(sanitized).toEqual({ name: 'u1', posts: [null] }); + }); + test('association black list', () => { const values = { name: 'username123', diff --git a/packages/core/database/src/update-guard.ts b/packages/core/database/src/update-guard.ts index bc5192609..de059b767 100644 --- a/packages/core/database/src/update-guard.ts +++ b/packages/core/database/src/update-guard.ts @@ -10,6 +10,7 @@ type UpdateValues = { }; type UpdateAction = 'create' | 'update'; + export class UpdateGuard { model: ModelStatic; action: UpdateAction; @@ -18,6 +19,21 @@ export class UpdateGuard { private blackList: BlackList; private whiteList: WhiteList; + static fromOptions(model, options) { + const guard = new UpdateGuard(); + guard.setModel(model); + guard.setWhiteList(options.whitelist); + guard.setBlackList(options.blacklist); + guard.setAction(lodash.get(options, 'action', 'update')); + guard.setAssociationKeysToBeUpdate(options.updateAssociationValues); + + if (options.underscored) { + guard.underscored = options.underscored; + } + + return guard; + } + setAction(action: UpdateAction) { this.action = action; } @@ -78,6 +94,10 @@ export class UpdateGuard { let associationValues = associationsValues[association]; const filterAssociationToBeUpdate = (value) => { + if (value === null) { + return value; + } + const associationKeysToBeUpdate = this.associationKeysToBeUpdate || []; if (associationKeysToBeUpdate.includes(association)) { @@ -155,19 +175,4 @@ export class UpdateGuard { return result; } - - static fromOptions(model, options) { - const guard = new UpdateGuard(); - guard.setModel(model); - guard.setWhiteList(options.whitelist); - guard.setBlackList(options.blacklist); - guard.setAction(lodash.get(options, 'action', 'update')); - guard.setAssociationKeysToBeUpdate(options.updateAssociationValues); - - if (options.underscored) { - guard.underscored = options.underscored; - } - - return guard; - } }