refactor - move validation into new module
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,3 @@
|
|||||||
lib
|
./lib
|
||||||
node_modules
|
node_modules
|
||||||
npm-debug.log
|
npm-debug.log
|
||||||
@@ -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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
20
src/index.js
20
src/index.js
@@ -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) => {
|
||||||
|
|||||||
17
src/lib/validate_schema.js
Normal file
17
src/lib/validate_schema.js
Normal 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;
|
||||||
Reference in New Issue
Block a user