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

Validation logic should probably not be in the db-code

This commit is contained in:
ivaosthu 2014-12-10 18:49:08 +01:00 committed by Ivar Conradi Østhus
parent ae0fdce395
commit b1ccb2e999
2 changed files with 15 additions and 16 deletions

View File

@ -5,6 +5,8 @@ var featureDb = require('./featureDb');
var NameExistsError = require('./error/NameExistsError');
var NotFoundError = require('./error/NotFoundError');
var ValidationError = require('./error/ValidationError');
var Promise = require("bluebird");
var requestValidator = require('./error/requestValidator');
module.exports = function (app) {
@ -28,7 +30,7 @@ module.exports = function (app) {
req.checkBody('name', 'Name must match format ^[a-zA-Z\\.\\-]+$').matches(/^[a-zA-Z\\.\\-]+$/i);
requestValidator(req)
.then(featureDb.validateUniqueName)
.then(validateUniqueName)
.then(function() {
return eventStore.create({
type: eventType.featureCreated,
@ -77,5 +79,16 @@ module.exports = function (app) {
res.status(500).end();
});
});
function validateUniqueName(req) {
return new Promise(function(resolve, reject) {
featureDb.getFeature(req.body.name)
.then(function() {
reject(new NameExistsError("Feature name already exist"));
}, function() {
resolve(req);
});
});
}
};

View File

@ -2,9 +2,7 @@ var eventStore = require('./eventStore');
var eventType = require('./eventType');
var logger = require('./logger');
var knex = require('./dbPool');
var Promise = require("bluebird");
var NotFoundError = require('./error/NotFoundError');
var NameExistsError = require('./error/NameExistsError');
var FEATURE_COLUMNS = ['name', 'description', 'enabled', 'strategy_name', 'parameters'];
eventStore.on(eventType.featureCreated, function (event) {
@ -64,20 +62,8 @@ function eventToRow(event) {
};
}
function validateUniqueName(req) {
return new Promise(function(resolve, reject) {
getFeature(req.body.name)
.then(function() {
reject(new NameExistsError("Feature name already exist"));
}, function() {
resolve(req);
});
});
}
module.exports = {
getFeatures: getFeatures,
getFeature: getFeature,
validateUniqueName: validateUniqueName
getFeature: getFeature
};