Skip to content

Commit

Permalink
feat: Merge pull request #48 from pelias/add-validation-to-generate
Browse files Browse the repository at this point in the history
added schema parameter to generate for auto-validation
  • Loading branch information
trescube authored Jan 25, 2017
2 parents aafab12 + efb6211 commit e91d94d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
22 changes: 17 additions & 5 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
{
"node": true,

"curly": true,
"latedef": true,
"quotmark": true,
"eqeqeq": true,
"esversion": 6,
"freeze": true,
"immed": true,
"indent": 2,
"latedef": false,
"newcap": true,
"noarg": true,
"noempty": true,
"nonbsp": true,
"nonew": true,
"plusplus": false,
"quotmark": "single",
"undef": true,
"unused": true,
"trailing": true
"unused": false,
"maxparams": 4,
"maxdepth": 4,
"maxlen": 140
}
34 changes: 31 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,37 @@ var fs = require('fs'),
Mergeable = require('mergeable'),
defaults = new Mergeable( __dirname + '/config/defaults.json' ),
localpath;
const _ = require('lodash');
const Joi = require('joi');

// allow the ops guys to override settings on the server
var generate = function( deep ){
var generate = function( schema, deep ){
// if first parameter is a boolean, then it's deep, not a schema
if (_.isBoolean(schema)) {
deep = schema;
schema = undefined;
}

// deep defaults to true
if (deep === undefined) {
deep = true;
}

const config = getConfig(deep);

if (_.isObject(schema)) {
Joi.validate(config, schema, (err) => {
if (err) {
throw new Error(err.details[0].message);
}
});
}

return config;

};

function getConfig(deep) {
// load config from ENV
if( process.env.hasOwnProperty('PELIAS_CONFIG') ){
var production = new Mergeable( defaults.export() );
Expand All @@ -25,11 +52,12 @@ var generate = function( deep ){
return local;
}
return defaults;
};

}

var config = {
defaults: defaults,
generate: generate.bind( null, true ),
generate: generate,
setLocalPath: function( p ){
localpath = p.replace( '~', process.env.HOME );
return localpath;
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
"elasticsearch": ">=1.1.1"
},
"dependencies": {
"joi": "^10.1.0",
"lodash": "^4.17.4",
"mergeable": "0.0.0"
},
"devDependencies": {
Expand Down
20 changes: 20 additions & 0 deletions test/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var path = require('path'),
config = require('../'),
defaults = require('../config/defaults');
const Joi = require('joi');

module.exports.generate = {};

Expand Down Expand Up @@ -183,6 +184,25 @@ module.exports.generate.paths = function(test) {
});
};

module.exports.generate.validate = (test) => {
test('non-validating schema should throw an error', (t) => {
t.throws(() => {
config.generate(Joi.boolean());
}, /"value" must be a boolean/);
t.end();

});

test('validating schema should not throw an error', (t) => {
t.doesNotThrow(() => {
config.generate(Joi.object().unknown(true));
});
t.end();

});

};

module.exports.all = function (tape) {

function test(name, testFunction) {
Expand Down

0 comments on commit e91d94d

Please sign in to comment.