From 969e3633f2a21223032a43cbb680fe0783fefba1 Mon Sep 17 00:00:00 2001 From: oleg Date: Mon, 28 Dec 2015 23:34:08 +0300 Subject: [PATCH] add basic project config validation --- lib/project.js | 55 ++++++++++++++++++++++++++++++++++++++++++-------- package.json | 3 ++- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/lib/project.js b/lib/project.js index 612534c..44dbf18 100644 --- a/lib/project.js +++ b/lib/project.js @@ -7,14 +7,55 @@ var Steppy = require('twostep').Steppy, reader = require('./reader'), db = require('../db'), utils = require('./utils'), - SpawnCommand = require('./command/spawn').Command; + SpawnCommand = require('./command/spawn').Command, + validateParams = require('./validateParams'); /** * Validates and returns given `config` to the `callback`(err, config) */ exports.validateConfig = function(config, callback) { - callback(null, config); + Steppy( + function() { + validateParams(config, { + type: 'object', + properties: { + scm: { + type: 'object', + required: true, + properties: { + type: {enum: ['git', 'mercurial'], required: true}, + repository: {type: 'string', required: true}, + rev: {type: 'string', required: true} + } + }, + steps: { + type: 'array', + required: true, + items: { + type: 'object', + properties: { + cmd: {type: 'string', required: true}, + shell: {type: 'string'} + } + } + } + }, + additionalProperties: true + }); + + this.pass(null); + }, + function(err) { + if (err) { + err.message = ( + 'Error during validation of project "' + config.name + + '": ' + err.message + ); + } + callback(err, config); + } + ); }; /** @@ -29,13 +70,11 @@ exports.load = function(baseDir, name, callback) { function(err, dirContent) { exports.loadConfig(dir, this.slot()); }, - function(err, config) { - exports.validateConfig(config, this.slot()); - }, function(err, config) { config.name = name; config.dir = dir; - this.pass(config); + + exports.validateConfig(config, this.slot()); }, callback ); @@ -79,10 +118,10 @@ exports.loadConfig = function(dir, callback) { }); } - // apply defaults + // apply defaults to not yet validated config _(config.steps).each(function(step) { if (!step.type) step.type = 'shell'; - if (!step.name) step.name = utils.prune(step.cmd, 40); + if (!step.name && step.cmd) step.name = utils.prune(step.cmd, 40); }); this.pass(config); diff --git a/package.json b/package.json index d444652..5e19dc6 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,8 @@ "socket.io": "1.3.5", "twostep": "0.4.1", "underscore": "1.8.3", - "through": "2.3.6" + "through": "2.3.6", + "conform": "0.2.12" }, "devDependencies": { "bower": "1.4.1",