1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

validate configures strategy shape

This commit is contained in:
sveisvei 2017-01-08 20:40:50 +01:00 committed by Ivar Conradi Østhus
parent bc93cd98bc
commit 86ac44908d

View File

@ -1,5 +1,6 @@
'use strict';
const joi = require('joi');
const logger = require('../logger');
const { FEATURE_CREATED, FEATURE_UPDATED, FEATURE_ARCHIVED } = require('../event-type');
const NameExistsError = require('../error/name-exists-error');
@ -148,11 +149,29 @@ module.exports = function (app, config) {
return Promise.resolve(req);
}
function validateStrategy (featureToggle) {
if (!featureToggle.strategies || featureToggle.strategies.length === 0) {
return Promise.reject(new ValidationError('You must define at least one strategy'));
}
return Promise.resolve(featureToggle);
const strategiesSchema = joi.object().keys({
name: joi.string()
.regex(/^[a-zA-Z0-9\\.\\-]{3,100}$/)
.required(),
parameters: joi.object(),
});
function validateStrategy (featureToggle) {
return new Promise((resolve, reject) => {
if (!featureToggle.strategies || featureToggle.strategies.length === 0) {
return reject(new ValidationError('You must define at least one strategy'));
}
featureToggle.strategies = featureToggle.strategies.map((strategyConfig) => {
const result = joi.validate(strategyConfig, strategiesSchema);
if (result.error) {
throw result.error;
}
return result.value;
});
return resolve(featureToggle);
});
}
};