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

View File

@@ -1,31 +1,17 @@
import events from 'events';
import Joi from 'joi';
import validateSchema from './lib/validate_schema';
export default class Elastique extends events.EventEmitter {
constructor(options = {}) {
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;
// initalize the module
Promise.all([
validateSchema()
validateSchema(options)
])
.then(([ settings ]) => {
console.log('settings', settings);
this.settings = settings;
})
.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;