From 86e9c35c3c720829e961fd057d202f5e720d0140 Mon Sep 17 00:00:00 2001 From: Nazar Gargol Date: Wed, 10 Oct 2018 15:52:08 +0200 Subject: [PATCH] Allowed passing an array directly instead of requiring object with values key for validation options noissue --- .../server/api/shared/validators/input/all.js | 20 ++++---- .../api/shared/validators/input/all_spec.js | 46 +++++++++++++++++++ 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/core/server/api/shared/validators/input/all.js b/core/server/api/shared/validators/input/all.js index 16d7e113b8..b5921a512f 100644 --- a/core/server/api/shared/validators/input/all.js +++ b/core/server/api/shared/validators/input/all.js @@ -38,16 +38,20 @@ const validate = (config, attrs) => { _.each(attrs, (value, key) => { debug(key, value); - if (config && config[key] && config[key].values) { - debug('ctrl validation'); + if (config && config[key]) { + const allowedValues = Array.isArray(config[key]) ? config[key] : config[key].values; - const valuesAsArray = value.trim().toLowerCase().split(','); - const unallowedValues = _.filter(valuesAsArray, (value) => { - return !config[key].values.includes(value); - }); + if (allowedValues) { + debug('ctrl validation'); - if (unallowedValues.length) { - errors.push(new common.errors.ValidationError()); + const valuesAsArray = value.trim().toLowerCase().split(','); + const unallowedValues = _.filter(valuesAsArray, (value) => { + return !allowedValues.includes(value); + }); + + if (unallowedValues.length) { + errors.push(new common.errors.ValidationError()); + } } } else if (GLOBAL_VALIDATORS[key]) { debug('global validation'); diff --git a/core/test/unit/api/shared/validators/input/all_spec.js b/core/test/unit/api/shared/validators/input/all_spec.js index 59af068595..1556230db8 100644 --- a/core/test/unit/api/shared/validators/input/all_spec.js +++ b/core/test/unit/api/shared/validators/input/all_spec.js @@ -38,6 +38,31 @@ describe('Unit: api/shared/validators/input/all', function () { }); }); + it('default include array notation', function () { + const frame = { + options: { + context: {}, + slug: 'slug', + include: 'tags,authors', + page: 2 + } + }; + + const apiConfig = { + options: { + include: ['tags', 'authors'] + } + }; + + return shared.validators.input.all(apiConfig, frame) + .then(() => { + should.exist(frame.options.page); + should.exist(frame.options.slug); + should.exist(frame.options.include); + should.exist(frame.options.context); + }); + }); + it('fails', function () { const frame = { options: { @@ -61,6 +86,27 @@ describe('Unit: api/shared/validators/input/all', function () { }); }); + it('fails include array notation', function () { + const frame = { + options: { + context: {}, + include: 'tags,authors' + } + }; + + const apiConfig = { + options: { + include: ['tags'] + } + }; + + return shared.validators.input.all(apiConfig, frame) + .then(Promise.reject) + .catch((err) => { + should.exist(err); + }); + }); + it('fails', function () { const frame = { options: {