From 7d4107fec4673023fd54a025a08d187d7e1625d5 Mon Sep 17 00:00:00 2001 From: kirrg001 Date: Sun, 8 May 2016 09:16:24 +0200 Subject: [PATCH] delete null values from incoming objects no issue - add more power to validation phase (checkObject) to get rid of null values --- core/server/api/utils.js | 9 +++++++++ core/test/unit/api_utils_spec.js | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/core/server/api/utils.js b/core/server/api/utils.js index 9401527314..7c787bb3a7 100644 --- a/core/server/api/utils.js +++ b/core/server/api/utils.js @@ -283,6 +283,15 @@ utils = { } } + // will remove unwanted null values + _.each(object[docName], function (value, index) { + if (!_.isObject(object[docName][index])) { + return; + } + + object[docName][index] = _.omit(object[docName][index], _.isNull); + }); + if (editId && object[docName][0].id && parseInt(editId, 10) !== parseInt(object[docName][0].id, 10)) { return errors.logAndRejectError(new errors.BadRequestError(i18n.t('errors.api.utils.invalidIdProvided'))); } diff --git a/core/test/unit/api_utils_spec.js b/core/test/unit/api_utils_spec.js index d79186b0d4..2d1dc230a9 100644 --- a/core/test/unit/api_utils_spec.js +++ b/core/test/unit/api_utils_spec.js @@ -370,6 +370,25 @@ describe('API Utils', function () { done(); }).catch(done); }); + + it('will delete null values from object', function (done) { + var object = {test: [{id: 1, key: null}]}; + + apiUtils.checkObject(_.cloneDeep(object), 'test').then(function (data) { + should.not.exist(data.test[0].key); + should.exist(data.test[0].id); + done(); + }).catch(done); + }); + + it('will not break if the expected object is a string', function (done) { + var object = {test: ['something']}; + + apiUtils.checkObject(_.cloneDeep(object), 'test').then(function (data) { + data.test[0].should.eql('something'); + done(); + }).catch(done); + }); }); describe('checkFileExists', function () {