-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
259 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
'use strict'; | ||
|
||
const MongoDBConfigError = require('./mongodb-config-error'); | ||
|
||
const MONGODB_CONFIG_STRUCT = { | ||
protocol: { | ||
type: 'string' | ||
}, | ||
host: { | ||
type: 'string' | ||
}, | ||
port: { | ||
type: 'number' | ||
}, | ||
user: { | ||
type: 'string' | ||
}, | ||
password: { | ||
type: 'string' | ||
}, | ||
database: { | ||
type: 'string', | ||
required: true | ||
}, | ||
limit: { | ||
type: 'number' | ||
} | ||
}; | ||
|
||
/** | ||
* @class ConfigValidator | ||
* @classdesc Validates config struct | ||
*/ | ||
class ConfigValidator { | ||
|
||
/** | ||
* Validate the received config struct | ||
* @throws if the struct is invalid | ||
*/ | ||
static validate(config) { | ||
|
||
if(!config || typeof config !== 'object' || Array.isArray(config)) | ||
throw new MongoDBConfigError('Invalid config: Should be an object.', MongoDBConfigError.codes.INVALID_CONFIG); | ||
|
||
for(const [setting, terms] of Object.entries(MONGODB_CONFIG_STRUCT)) { | ||
|
||
switch(typeof config[setting]) { | ||
|
||
case terms.type: | ||
break; | ||
|
||
case 'undefined': | ||
if(terms.required) | ||
throw new MongoDBConfigError(`Invalid config: '${setting}' is required.`, MongoDBConfigError.codes.REQUIRED_SETTING); | ||
break; | ||
|
||
default: | ||
throw new MongoDBConfigError(`Invalid setting '${setting}': Expected ${terms.type} but received ${typeof config[setting]}.`, | ||
MongoDBConfigError.codes.INVALID_SETTING); | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
module.exports = ConfigValidator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
|
||
class MongoDBConfigError extends Error { | ||
|
||
static get codes() { | ||
|
||
return { | ||
INVALID_CONFIG: 1, | ||
INVALID_SETTING: 2, | ||
REQUIRED_SETTING: 3 | ||
}; | ||
|
||
} | ||
|
||
constructor(err, code) { | ||
super(err); | ||
this.message = err.message || err; | ||
this.code = code; | ||
this.name = 'MongoDBConfigError'; | ||
} | ||
} | ||
|
||
module.exports = MongoDBConfigError; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
const ConfigValidator = require('./../lib/config-validator'); | ||
|
||
const { MongoDBConfigError } = require('./../lib'); | ||
|
||
describe('ConfigValidator', () => { | ||
|
||
describe('validate()', () => { | ||
|
||
it('should throw invalid config when the config is empty', () => { | ||
|
||
assert.throws(() => ConfigValidator.validate(), { | ||
name: 'MongoDBConfigError', | ||
code: MongoDBConfigError.codes.INVALID_CONFIG | ||
}); | ||
|
||
}); | ||
|
||
|
||
it('should throw invalid config when the config is not an object', () => { | ||
|
||
assert.throws(() => ConfigValidator.validate('string'), { | ||
name: 'MongoDBConfigError', | ||
code: MongoDBConfigError.codes.INVALID_CONFIG | ||
}); | ||
|
||
}); | ||
|
||
|
||
it('should throw invalid config when the config is an array', () => { | ||
|
||
assert.throws(() => ConfigValidator.validate([]), { | ||
name: 'MongoDBConfigError', | ||
code: MongoDBConfigError.codes.INVALID_CONFIG | ||
}); | ||
|
||
}); | ||
|
||
it('should throw required setting when a required setting is missing', () => { | ||
|
||
assert.throws(() => ConfigValidator.validate({}), { | ||
name: 'MongoDBConfigError', | ||
code: MongoDBConfigError.codes.REQUIRED_SETTING | ||
}); | ||
|
||
}); | ||
|
||
|
||
['string', ['array']].forEach(type => { | ||
|
||
it('should throw invalid setting when a setting has an unexpected type', () => { | ||
|
||
assert.throws(() => ConfigValidator.validate({ | ||
database: 'myDB', | ||
port: type | ||
}), { | ||
name: 'MongoDBConfigError', | ||
code: MongoDBConfigError.codes.INVALID_SETTING | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
it('should not throw when the settings are correct', () => { | ||
|
||
assert.doesNotThrow(() => ConfigValidator.validate({ | ||
database: 'myDB', | ||
port: 27017 | ||
})); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.