refactor - move validation into new module

This commit is contained in:
2016-04-14 15:07:20 -07:00
parent 5619612943
commit b0e0f9452b
4 changed files with 22 additions and 18 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,3 @@
lib ./lib
node_modules node_modules
npm-debug.log npm-debug.log

View File

@@ -23,6 +23,7 @@
"sinon": "~1.17.3" "sinon": "~1.17.3"
}, },
"dependencies": { "dependencies": {
"elasticsearch": "~11.0.1",
"joi": "~8.0.5" "joi": "~8.0.5"
} }
} }

View File

@@ -1,31 +1,17 @@
import events from 'events'; import events from 'events';
import Joi from 'joi'; import validateSchema from './lib/validate_schema';
export default class Elastique extends events.EventEmitter { export default class Elastique extends events.EventEmitter {
constructor(options = {}) { constructor(options = {}) {
super(); super();
var schema = Joi.object({
url: Joi.string().uri({ scheme: ['http', 'https'] }).default('http://localhost:9200'),
index: Joi.string().required(),
interval: Joi.string().default('1w'),
timeout: Joi.number().min(10000).default(10000),
}).default();
const validateSchema = () => new Promise(function (resolve, reject) {
schema.validate(options, (err, settings) => {
if (err) return reject(err);
resolve(settings);
})
})
this.ready = true; this.ready = true;
// initalize the module
Promise.all([ Promise.all([
validateSchema() validateSchema(options)
]) ])
.then(([ settings ]) => { .then(([ settings ]) => {
console.log('settings', settings);
this.settings = settings; this.settings = settings;
}) })
.catch((err) => { .catch((err) => {

View File

@@ -0,0 +1,17 @@
import Joi from 'joi';
var schema = Joi.object({
url: Joi.string().uri({ scheme: ['http', 'https'] }).default('http://localhost:9200'),
index: Joi.string().required(),
interval: Joi.string().default('1w'),
timeout: Joi.number().min(10000).default(10000),
}).default();
const validateSchema = (options) => new Promise(function (resolve, reject) {
schema.validate(options, (err, settings) => {
if (err) return reject(err);
resolve(settings);
})
})
export default validateSchema;