How often you need to cope with situations like this?
function init(config) {
myOption || (myOption = parseInt(config.myOption, 10));
(...)
TypeError: Cannot read property 'myOption' of null
// NO MORE !!!
Wouldn't it be easier to keep all your configuration schemas in one place and delegate all the validation hassle to external library (like cruks-lib-config)?
var expectedConfiguration = expect.schema({
"myOption": expect.integer().default(10)
});
function init(config) {
config = expectedConfiguration.assert(config);
config.myOption; // 10! Much better!
}
Configuration processor asserts that any input matches your expected parameters and makes validating complicated objects much easier. It also provides some basic assertion tools and verbose debug messages.
Focus on clear, readable messages and ability to track exact failing element makes cruks-lib-config really special.
Every array item needs to be integer.
expect.array.every(expect.integer()).assert([1, "2", 3]);
Result:
AssertionFailureError: expected integer but "1" got (string)"2"
// "1" is the name of affected array key
expect = require("cruks-lib-config").expect;
expectedConfiguration = exepct.schema({
"a": expect.number(),
"b": expect.string(),
"c": expect.schema({
"d": expect.schema({
"e": expect.boolean()
})
}),
"f": expect.array()
});
expectedConfiguration.assert({
"a": 5,
"b": "some-string",
"c": {
"d": {
"e": "not-a-boolean"
}
},
"f": []
});
Result:
AssertionFailureError: expected boolean but "c.d.e" got (string)"not-a-boolean"
For more information please refer to project's wiki.