Skip to content

Commit

Permalink
utils: don't filter values when selecting with wildcards
Browse files Browse the repository at this point in the history
Wildcards may yield 0..* instances, each with a
different path, so we may want to skip such
filtering.

Fixes #531
Fixes #458
  • Loading branch information
gustavohenke committed Apr 9, 2018
1 parent 3ecc008 commit ba8b045
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 28 deletions.
6 changes: 5 additions & 1 deletion utils/select-fields.js
Expand Up @@ -13,7 +13,11 @@ module.exports = (req, context, options = {}) => {
.filter(optionalityFilter)
.value();

if (instances.length > 1 && context.locations.length > 1) {
// #331 - When multiple locations are involved, all of them must pass the validation.
// If none of the locations contain the field, we at least include one for error reporting.
// #458, #531 - Wildcards are an exception though: they may yield 0..* instances with different
// paths, so we may want to skip this filtering.
if (instances.length > 1 && context.locations.length > 1 && !field.includes('*')) {
const withValue = instances.filter(field => field.value !== undefined);
instances = withValue.length ? withValue : [instances[0]];
}
Expand Down
73 changes: 46 additions & 27 deletions utils/select-fields.spec.js
Expand Up @@ -2,33 +2,6 @@ const { expect } = require('chai');
const selectFields = require('./select-fields');

describe('utils: selectFields', () => {
it('is done in all given request locations', () => {
const req = {
body: { foo: 'a' },
params: { foo: 'b' },
query: { foo: 'c' }
};

const instances = selectFields(req, {
locations: ['body', 'query'],
fields: ['foo']
});

expect(instances).to.have.length(2);
expect(instances).to.deep.include({
location: 'body',
path: 'foo',
originalValue: 'a',
value: 'a'
});
expect(instances).to.deep.include({
location: 'query',
path: 'foo',
originalValue: 'c',
value: 'c'
});
});

it('accepts multiple fields using array', () => {
const req = {
query: { a: 'ASD', b: 'BCA' }
Expand Down Expand Up @@ -327,6 +300,33 @@ describe('utils: selectFields', () => {
});

describe('when there are multiple locations', () => {
it('is done in all of them', () => {
const req = {
body: { foo: 'a' },
params: { foo: 'b' },
query: { foo: 'c' }
};

const instances = selectFields(req, {
locations: ['body', 'query'],
fields: ['foo']
});

expect(instances).to.have.length(2);
expect(instances).to.deep.include({
location: 'body',
path: 'foo',
originalValue: 'a',
value: 'a'
});
expect(instances).to.deep.include({
location: 'query',
path: 'foo',
originalValue: 'c',
value: 'c'
});
});

it('ignores those which do not have value in case others do', () => {
const req = {
body: { foo: 'a' },
Expand Down Expand Up @@ -366,5 +366,24 @@ describe('utils: selectFields', () => {
value: undefined
});
});

it('includes all occurrences when there is a wildcard', () => {
const req = {
body: { foo: [{ bar: 0 }, {}] },
cookies: { foo: [{ bar: 0 }, { baz: [{}] }] },
};

const instances = selectFields(req, {
fields: ['foo.*.bar', 'foo.*.baz.*.qux'],
locations: ['body', 'cookies']
});

expect(instances).to.have.length(5);
expect(instances[0]).to.include({ path: 'foo[0].bar', location: 'body' });
expect(instances[1]).to.include({ path: 'foo[1].bar', location: 'body' });
expect(instances[2]).to.include({ path: 'foo[0].bar', location: 'cookies' });
expect(instances[3]).to.include({ path: 'foo[1].bar', location: 'cookies' });
expect(instances[4]).to.include({ path: 'foo[1].baz[0].qux', location: 'cookies' });
});
});
});

0 comments on commit ba8b045

Please sign in to comment.