0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Allowed passing an array directly instead of requiring object with values key for validation options

noissue
This commit is contained in:
Nazar Gargol 2018-10-10 15:52:08 +02:00
parent eb0bc3068c
commit 86e9c35c3c
2 changed files with 58 additions and 8 deletions

View file

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

View file

@ -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: {