Skip to content

Commit

Permalink
utils: persist empty paths as the req location itself
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavohenke committed Jun 30, 2018
1 parent a30490b commit 878db44
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion utils/persist-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module.exports = (req, fieldInstances) => {
const initialValue = _.get(req[instance.location], instance.path);
return initialValue !== instance.value;
}).forEach(instance => {
_.set(req[instance.location], instance.path, instance.value);
instance.path === ''
? _.set(req, instance.location, instance.value)
: _.set(req[instance.location], instance.path, instance.value);
});
};
7 changes: 7 additions & 0 deletions utils/persist-values.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ describe('utils: persistValues', () => {
expect(req.body.bar.baz).to.equal('qux');
});

it('persists empty paths as the req location itself', () => {
const req = {};
persistValues(req, [{ location: 'body', path: '', value: 'noice' }]);

expect(req.body).to.equal('noice');
});

it('does not persist undefined values into req when the key does not exist', () => {
const req = {
query: {}
Expand Down

0 comments on commit 878db44

Please sign in to comment.