Browse Source

converted configValidation to basic schema object

to inject into `config.generate`

fixed tests
pull/788/head
Stephen Hess 8 years ago
parent
commit
2288e227a3
  1. 13
      schema.js
  2. 2
      test/unit/run.js
  3. 188
      test/unit/schema.js

13
src/configValidation.js → schema.js

@ -14,7 +14,7 @@ const Joi = require('joi');
// * api.relativeScores (boolean) // * api.relativeScores (boolean)
// * api.legacyUrl (string) // * api.legacyUrl (string)
// * api.localization (flipNumberAndStreetCountries is array of 3 character strings) // * api.localization (flipNumberAndStreetCountries is array of 3 character strings)
const schema = Joi.object().keys({ module.exports = Joi.object().keys({
api: Joi.object().keys({ api: Joi.object().keys({
version: Joi.string(), version: Joi.string(),
indexName: Joi.string(), indexName: Joi.string(),
@ -32,14 +32,3 @@ const schema = Joi.object().keys({
requestTimeout: Joi.number().integer().min(0) requestTimeout: Joi.number().integer().min(0)
}).unknown(true) }).unknown(true)
}).requiredKeys('api', 'esclient').unknown(true); }).requiredKeys('api', 'esclient').unknown(true);
module.exports = {
validate: function validate(config) {
Joi.validate(config, schema, (err) => {
if (err) {
throw new Error(err.details[0].message);
}
});
}
};

2
test/unit/run.js

@ -10,6 +10,7 @@ var common = {
var tests = [ var tests = [
require('./app'), require('./app'),
require('./schema'),
require('./controller/index'), require('./controller/index'),
require('./controller/place'), require('./controller/place'),
require('./controller/search'), require('./controller/search'),
@ -62,7 +63,6 @@ var tests = [
require('./sanitizer/_deprecate_quattroshapes'), require('./sanitizer/_deprecate_quattroshapes'),
require('./sanitizer/_categories'), require('./sanitizer/_categories'),
require('./sanitizer/nearby'), require('./sanitizer/nearby'),
require('./src/configValidation'),
require('./sanitizer/autocomplete'), require('./sanitizer/autocomplete'),
require('./sanitizer/structured_geocoding'), require('./sanitizer/structured_geocoding'),
require('./sanitizer/place'), require('./sanitizer/place'),

188
test/unit/src/configValidation.js → test/unit/schema.js

@ -1,11 +1,20 @@
'use strict'; 'use strict';
const configValidation = require('../../../src/configValidation'); const Joi = require('joi');
const schema = require('../../schema');
function validate(config) {
Joi.validate(config, schema, (err, value) => {
if (err) {
throw new Error(err.details[0].message);
}
});
}
module.exports.tests = {}; module.exports.tests = {};
module.exports.tests.completely_valid = function(test, common) { module.exports.tests.completely_valid = (test, common) => {
test('all valid configuration elements should not throw error', function(t) { test('all valid configuration elements should not throw error', (t) => {
var config = { var config = {
api: { api: {
version: 'version value', version: 'version value',
@ -24,15 +33,12 @@ module.exports.tests.completely_valid = function(test, common) {
} }
}; };
t.doesNotThrow(function() { t.doesNotThrow(validate.bind(config));
configValidation.validate(config);
});
t.end(); t.end();
}); });
test('basic valid configurtaion should not throw error', function(t) { test('basic valid configuration should not throw error', (t) => {
var config = { var config = {
api: { api: {
version: 'version value', version: 'version value',
@ -44,30 +50,25 @@ module.exports.tests.completely_valid = function(test, common) {
} }
}; };
t.doesNotThrow(function() { t.doesNotThrow(validate.bind(config));
configValidation.validate(config);
});
t.end(); t.end();
}); });
}; };
module.exports.tests.api_validation = function(test, common) { module.exports.tests.api_validation = (test, common) => {
test('config without api should throw error', function(t) { test('config without api should throw error', (t) => {
var config = { var config = {
esclient: {} esclient: {}
}; };
t.throws(function() { t.throws(validate.bind(null, config), /"api" is required/, 'api should exist');
configValidation.validate(config);
}, /"api" is required/, 'api should exist');
t.end(); t.end();
}); });
test('config without unknown field in api should not throw error', function(t) { test('config without unknown field in api should not throw error', (t) => {
var config = { var config = {
api: { api: {
version: 'version value', version: 'version value',
@ -78,14 +79,12 @@ module.exports.tests.api_validation = function(test, common) {
esclient: {} esclient: {}
}; };
t.doesNotThrow(function() { t.doesNotThrow(validate.bind(null, config), 'unknown properties should be allowed');
configValidation.validate(config);
}, 'unknown properties should be allowed');
t.end(); t.end();
}); });
test('non-string api.version should throw error', function(t) { test('non-string api.version should throw error', (t) => {
[null, 17, {}, [], true].forEach((value) => { [null, 17, {}, [], true].forEach((value) => {
var config = { var config = {
api: { api: {
@ -96,16 +95,15 @@ module.exports.tests.api_validation = function(test, common) {
esclient: {} esclient: {}
}; };
t.throws(function() { t.throws(validate.bind(null, config), /"version" must be a string/);
configValidation.validate(config);
}, /"version" must be a string/);
}); });
t.end(); t.end();
}); });
test('non-string api.indexName should throw error', function(t) { test('non-string api.indexName should throw error', (t) => {
[null, 17, {}, [], true].forEach((value) => { [null, 17, {}, [], true].forEach((value) => {
var config = { var config = {
api: { api: {
@ -116,16 +114,15 @@ module.exports.tests.api_validation = function(test, common) {
esclient: {} esclient: {}
}; };
t.throws(function() { t.throws(validate.bind(null, config), /"indexName" must be a string/);
configValidation.validate(config);
}, /"indexName" must be a string/);
}); });
t.end(); t.end();
}); });
test('non-string api.host should throw error', function(t) { test('non-string api.host should throw error', (t) => {
[null, 17, {}, [], true].forEach((value) => { [null, 17, {}, [], true].forEach((value) => {
var config = { var config = {
api: { api: {
@ -136,16 +133,15 @@ module.exports.tests.api_validation = function(test, common) {
esclient: {} esclient: {}
}; };
t.throws(function() { t.throws(validate.bind(null, config), /"host" must be a string/);
configValidation.validate(config);
}, /"host" must be a string/);
}); });
t.end(); t.end();
}); });
test('non-string api.legacyUrl should throw error', function(t) { test('non-string api.legacyUrl should throw error', (t) => {
[null, 17, {}, [], true].forEach((value) => { [null, 17, {}, [], true].forEach((value) => {
var config = { var config = {
api: { api: {
@ -157,16 +153,15 @@ module.exports.tests.api_validation = function(test, common) {
esclient: {} esclient: {}
}; };
t.throws(function() { t.throws(validate.bind(null, config), /"legacyUrl" must be a string/);
configValidation.validate(config);
}, /"legacyUrl" must be a string/);
}); });
t.end(); t.end();
}); });
test('non-string api.accessLog should throw error', function(t) { test('non-string api.accessLog should throw error', (t) => {
[null, 17, {}, [], true].forEach((value) => { [null, 17, {}, [], true].forEach((value) => {
var config = { var config = {
api: { api: {
@ -178,16 +173,15 @@ module.exports.tests.api_validation = function(test, common) {
esclient: {} esclient: {}
}; };
t.throws(function() { t.throws(validate.bind(null, config), /"accessLog" must be a string/);
configValidation.validate(config);
}, /"accessLog" must be a string/);
}); });
t.end(); t.end();
}); });
test('non-boolean api.relativeScores should throw error', function(t) { test('non-boolean api.relativeScores should throw error', (t) => {
[null, 17, {}, [], 'string'].forEach((value) => { [null, 17, {}, [], 'string'].forEach((value) => {
var config = { var config = {
api: { api: {
@ -199,16 +193,15 @@ module.exports.tests.api_validation = function(test, common) {
esclient: {} esclient: {}
}; };
t.throws(function() { t.throws(validate.bind(null, config), /"relativeScores" must be a boolean/);
configValidation.validate(config);
}, /"relativeScores" must be a boolean/);
}); });
t.end(); t.end();
}); });
test('non-object api.localization should throw error', function(t) { test('non-object api.localization should throw error', (t) => {
[null, 17, false, [], 'string'].forEach((value) => { [null, 17, false, [], 'string'].forEach((value) => {
var config = { var config = {
api: { api: {
@ -220,16 +213,15 @@ module.exports.tests.api_validation = function(test, common) {
esclient: {} esclient: {}
}; };
t.throws(function() { t.throws(validate.bind(null, config), /"localization" must be an object/);
configValidation.validate(config);
}, /"localization" must be an object/);
}); });
t.end(); t.end();
}); });
test('unknown properties in api.localization should throw error', function(t) { test('unknown properties in api.localization should throw error', (t) => {
var config = { var config = {
api: { api: {
version: 'version value', version: 'version value',
@ -242,15 +234,13 @@ module.exports.tests.api_validation = function(test, common) {
esclient: {} esclient: {}
}; };
t.throws(function() { t.throws(validate.bind(null, config), /"unknown_property" is not allowed/);
configValidation.validate(config);
}, /"unknown_property" is not allowed/);
t.end(); t.end();
}); });
test('non-array api.localization.flipNumberAndStreetCountries should throw error', function(t) { test('non-array api.localization.flipNumberAndStreetCountries should throw error', (t) => {
[null, 17, {}, false, 'string'].forEach((value) => { [null, 17, {}, false, 'string'].forEach((value) => {
var config = { var config = {
api: { api: {
@ -264,16 +254,17 @@ module.exports.tests.api_validation = function(test, common) {
esclient: {} esclient: {}
}; };
t.throws(function() { t.throws(
configValidation.validate(config); validate.bind(null, config),
}, /"flipNumberAndStreetCountries" must be an array/); /"flipNumberAndStreetCountries" must be an array/);
}); });
t.end(); t.end();
}); });
test('non-string api.localization.flipNumberAndStreetCountries elements should throw error', function(t) { test('non-string api.localization.flipNumberAndStreetCountries elements should throw error', (t) => {
[null, 17, {}, false, []].forEach((value) => { [null, 17, {}, false, []].forEach((value) => {
var config = { var config = {
api: { api: {
@ -287,16 +278,15 @@ module.exports.tests.api_validation = function(test, common) {
esclient: {} esclient: {}
}; };
t.throws(function() { t.throws(validate.bind(null, config), /"0" must be a string/);
configValidation.validate(config);
}, /"0" must be a string/);
}); });
t.end(); t.end();
}); });
test('non-3-char api.localization.flipNumberAndStreetCountries elements should throw error', function(t) { test('non-3-char api.localization.flipNumberAndStreetCountries elements should throw error', (t) => {
['AB', 'ABCD'].forEach((value) => { ['AB', 'ABCD'].forEach((value) => {
var config = { var config = {
api: { api: {
@ -310,16 +300,14 @@ module.exports.tests.api_validation = function(test, common) {
esclient: {} esclient: {}
}; };
t.throws(function() { t.throws(validate.bind(null, config), /fails to match the required pattern/);
configValidation.validate(config);
}, /fails to match the required pattern/);
}); });
t.end(); t.end();
}); });
test('config with non-number api.requestRetries should throw error', function(t) { test('config with non-number api.requestRetries should throw error', (t) => {
[null, 'string', {}, [], false].forEach((value) => { [null, 'string', {}, [], false].forEach((value) => {
var config = { var config = {
api: { api: {
@ -331,16 +319,17 @@ module.exports.tests.api_validation = function(test, common) {
esclient: {} esclient: {}
}; };
t.throws(function() { t.throws(
configValidation.validate(config); validate.bind(null, config),
}, /"requestRetries" must be a number/, 'api.requestRetries should be a number'); /"requestRetries" must be a number/, 'api.requestRetries should be a number');
}); });
t.end(); t.end();
}); });
test('config with non-integer api.requestRetries should throw error', function(t) { test('config with non-integer api.requestRetries should throw error', (t) => {
var config = { var config = {
api: { api: {
version: 'version value', version: 'version value',
@ -351,15 +340,15 @@ module.exports.tests.api_validation = function(test, common) {
esclient: {} esclient: {}
}; };
t.throws(function() { t.throws(
configValidation.validate(config); validate.bind(null, config),
}, /"requestRetries" must be an integer/, 'api.requestRetries should be an integer'); /"requestRetries" must be an integer/, 'api.requestRetries should be an integer');
t.end(); t.end();
}); });
test('config with negative api.requestRetries should throw error', function(t) { test('config with negative api.requestRetries should throw error', (t) => {
var config = { var config = {
api: { api: {
version: 'version value', version: 'version value',
@ -370,9 +359,9 @@ module.exports.tests.api_validation = function(test, common) {
esclient: {} esclient: {}
}; };
t.throws(function() { t.throws(
configValidation.validate(config); validate.bind(null, config),
}, /"requestRetries" must be larger than or equal to 0/, 'api.requestRetries must be positive'); /"requestRetries" must be larger than or equal to 0/, 'api.requestRetries must be positive');
t.end(); t.end();
@ -380,8 +369,8 @@ module.exports.tests.api_validation = function(test, common) {
}; };
module.exports.tests.esclient_validation = function(test, common) { module.exports.tests.esclient_validation = (test, common) => {
test('config without esclient should throw error', function(t) { test('config without esclient should throw error', (t) => {
var config = { var config = {
api: { api: {
version: 'version value', version: 'version value',
@ -390,14 +379,14 @@ module.exports.tests.esclient_validation = function(test, common) {
} }
}; };
t.throws(function() { t.throws(
configValidation.validate(config); validate.bind(null, config),
}, /"esclient" is required/, 'esclient should exist'); /"esclient" is required/, 'esclient should exist');
t.end(); t.end();
}); });
test('config with non-object esclient should throw error', function(t) { test('config with non-object esclient should throw error', (t) => {
[null, 17, [], 'string', true].forEach((value) => { [null, 17, [], 'string', true].forEach((value) => {
var config = { var config = {
api: { api: {
@ -408,9 +397,9 @@ module.exports.tests.esclient_validation = function(test, common) {
esclient: value esclient: value
}; };
t.throws(function() { t.throws(
configValidation.validate(config); validate.bind(null, config),
}, /"esclient" must be an object/, 'esclient should be an object'); /"esclient" must be an object/, 'esclient should be an object');
}); });
@ -418,7 +407,7 @@ module.exports.tests.esclient_validation = function(test, common) {
}); });
test('config with non-number esclient.requestTimeout should throw error', function(t) { test('config with non-number esclient.requestTimeout should throw error', (t) => {
[null, 'string', {}, [], false].forEach((value) => { [null, 'string', {}, [], false].forEach((value) => {
var config = { var config = {
api: { api: {
@ -431,16 +420,17 @@ module.exports.tests.esclient_validation = function(test, common) {
} }
}; };
t.throws(function() { t.throws(
configValidation.validate(config); validate.bind(null, config),
}, /"requestTimeout" must be a number/, 'esclient.requestTimeout should be a number'); /"requestTimeout" must be a number/, 'esclient.requestTimeout should be a number');
}); });
t.end(); t.end();
}); });
test('config with non-integer esclient.requestTimeout should throw error', function(t) { test('config with non-integer esclient.requestTimeout should throw error', (t) => {
var config = { var config = {
api: { api: {
version: 'version value', version: 'version value',
@ -452,15 +442,15 @@ module.exports.tests.esclient_validation = function(test, common) {
} }
}; };
t.throws(function() { t.throws(
configValidation.validate(config); validate.bind(null, config),
}, /"requestTimeout" must be an integer/, 'esclient.requestTimeout should be an integer'); /"requestTimeout" must be an integer/, 'esclient.requestTimeout should be an integer');
t.end(); t.end();
}); });
test('config with negative esclient.requestTimeout should throw error', function(t) { test('config with negative esclient.requestTimeout should throw error', (t) => {
var config = { var config = {
api: { api: {
version: 'version value', version: 'version value',
@ -472,9 +462,9 @@ module.exports.tests.esclient_validation = function(test, common) {
} }
}; };
t.throws(function() { t.throws(
configValidation.validate(config); validate.bind(null, config),
}, /"requestTimeout" must be larger than or equal to 0/, 'esclient.requestTimeout must be positive'); /"requestTimeout" must be larger than or equal to 0/, 'esclient.requestTimeout must be positive');
t.end(); t.end();
@ -482,7 +472,7 @@ module.exports.tests.esclient_validation = function(test, common) {
}; };
module.exports.all = function (tape, common) { module.exports.all = (tape, common) => {
function test(name, testFunction) { function test(name, testFunction) {
return tape('configValidation: ' + name, testFunction); return tape('configValidation: ' + name, testFunction);
Loading…
Cancel
Save