diff --git a/filter/matched-data.js b/filter/matched-data.js index 3f30d2f5..350d87d5 100644 --- a/filter/matched-data.js +++ b/filter/matched-data.js @@ -4,9 +4,6 @@ const selectFields = require('../check/select-fields'); module.exports = (req, options = {}) => { let onlyValidData = options.onlyValidData === undefined ? true : options.onlyValidData let customLocations = options.locations === undefined ? false : options.locations - if (customLocations && !Array.isArray(customLocations)) { - throw new Error('returnLocations must be an Array') - } const validityFilter = !onlyValidData ? () => true : field => { return !req._validationErrors.find(error => @@ -15,7 +12,7 @@ module.exports = (req, options = {}) => { ); }; - const locationsFilter = !customLocations ? () => true : v => { + const locationsFilter = !customLocations || customLocations.length === 0 ? () => true : v => { return customLocations.indexOf(v.location) !== -1 } diff --git a/filter/matched-data.spec.js b/filter/matched-data.spec.js index cd8f7ab3..42c22847 100644 --- a/filter/matched-data.spec.js +++ b/filter/matched-data.spec.js @@ -98,7 +98,7 @@ describe('filter: matchedData', () => { }); }); - it('returns object with the specified location in the array', () => { + it('returns object with the specified locations in the array', () => { const req = { query: { foo: '123', bar: 'abc' }, body: { baz: '234' } @@ -117,5 +117,25 @@ describe('filter: matchedData', () => { }); }); }); + + it('returns object all the specified locations if not specified', () => { + const req = { + query: { foo: '123', bar: 'abc' }, + body: { baz: '234' } + }; + + return check(['foo', 'bar', 'baz']).isInt()(req, {}, () => {}).then(() => { + const data = matchedData(req, { + onlyValidData: false, + locations: [] + }); + + expect(data).to.eql({ + foo: '123', + bar: 'abc', + baz: '234' + }); + }); + }); }); });