mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-28 00:06:53 +01:00
Merge pull request #200 from Unleash/validate-configured-strategy-shape
validate configures strategy shape
This commit is contained in:
commit
419380259d
@ -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);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user