delete null values from incoming objects

no issue
- add more power to validation phase (checkObject) to get rid of null values
This commit is contained in:
kirrg001 2016-05-08 09:16:24 +02:00
parent dac44b4d4b
commit 7d4107fec4
2 changed files with 28 additions and 0 deletions

View File

@ -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')));
}

View File

@ -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 () {