Comparing version 0.1.1 to 0.2.1
@@ -10,4 +10,4 @@ { | ||
"devDependencies": { | ||
"jasmine-node": "1.0.x", | ||
"sinon": "1.5.x" | ||
"jasmine-node": "1.7.x", | ||
"sinon": "1.7.x" | ||
}, | ||
@@ -20,4 +20,7 @@ "keywords": ["util", "functional", "server", "sanity"], | ||
}, | ||
"scripts": { | ||
"test": "jasmine-node test/" | ||
}, | ||
"main": "sanity.js", | ||
"version": "0.1.1" | ||
"version": "0.2.1" | ||
} |
@@ -15,7 +15,14 @@ # Sanity | ||
sanity.check( | ||
['array', 'of', 'keys'], | ||
/* optional data source {gein: 'clown'}, */ | ||
/* optional configuration object: {passiveAggressive: true}, */ | ||
/* optional callback: function(err, keys){ if(err) alert(keys.join(', ')); } */ | ||
) | ||
['array', 'of', 'keys'] | ||
// Options | ||
, | ||
{ | ||
gagged: false, // "true" will prevent any output | ||
goodBook: null, // Provide an object literal to set default values to "source" | ||
passiveAggressive: false, // "true" will not stop app if validation fails | ||
recover: null, // If a function is provided it is called if validation fails | ||
source: process.env, // Want to configure another object? Stick it in here. e.g. {gein: 'clown'} | ||
zazz: true // "false" will show everyone you're a boring person | ||
} | ||
); | ||
``` | ||
@@ -71,3 +78,7 @@ | ||
``` js | ||
var sanity = require('sanity'); | ||
var sanity = require('sanity'), | ||
source = { | ||
GOTTI_BURIAL_LOCATION: app.unveilTruth('gotti'), | ||
UNDERCOVER_AGENT: db.get('user', 'type = "undercover"') | ||
}; | ||
@@ -77,5 +88,5 @@ sanity.check( | ||
{ | ||
GOTTI_BURIAL_LOCATION: app.unveilTruth('gotti'), | ||
UNDERCOVER_AGENT: db.get('user', 'type = "undercover"') | ||
}); | ||
source: source | ||
} | ||
); | ||
@@ -90,8 +101,9 @@ // Theoretical output | ||
``` js | ||
var sanity = require('sanity'); | ||
var sanity = require('sanity'), | ||
options = { | ||
gagged: true, // default: false | ||
passiveAggressive: true // default: false | ||
}; | ||
sanity.check(['ONE_ARMED_MAN'], null, { | ||
gagged: true, // default: false | ||
passiveAggressive: true // default: false | ||
}); | ||
sanity.check(['ONE_ARMED_MAN'], options); | ||
@@ -104,9 +116,12 @@ // ONE_ARMED_MAN is falsy but `gagged` prevents logging and `passiveAggressive` does not exit the process | ||
``` js | ||
var sanity = require('sanity'); | ||
var sanity = require('sanity'), | ||
options = { | ||
recover: function(err, keys) { | ||
console.error(err); // Same error format as seen before | ||
console.log(keys) // Array of keys which did not pass | ||
process.exit(1); | ||
} | ||
}; | ||
sanity.check(['UFOS'], null, null, function(err, keys) { | ||
console.error(err); // Same error format as seen before | ||
console.log(keys) // Array of keys which did not pass | ||
process.exit(1); | ||
}); | ||
sanity.check(['UFOS'], opttions); | ||
@@ -120,3 +135,6 @@ ``` | ||
* gagged: Truthy value prevents the reporter from being called. Could be useful in test environments. | ||
* goodBook: An object literal that, if provided, is used automatically to prepopulate the `source`. | ||
* passiveAggressive: If truthy and no callback provided this prevents sanity from running `process.exit(1)` when errors are found. | ||
* recover(message, failedKeys): A funciton that is invoked if validation fails. | ||
* source: Defaults to `process.env` but you can provide any source against which to test keys. | ||
* zazz: Falsy value stops the reported text from looking zazzy. | ||
@@ -128,2 +146,2 @@ | ||
jasmine-node test/ | ||
npm test |
@@ -22,12 +22,18 @@ 'use strict'; | ||
}, | ||
check: function(required, source, options, cb) { | ||
check: function(required, options) { | ||
var failures = [], | ||
source = source || process.env, | ||
message = ''; | ||
options = _.extend({ | ||
gagged: false, | ||
goodBook: null, | ||
passiveAggressive: false, | ||
recover: null, | ||
source: process.env, | ||
zazz: true | ||
}, options); | ||
if (options.goodBook) { | ||
sanity.preach(options.goodBook, options.source); | ||
} | ||
required.forEach(function(key) { | ||
@@ -41,4 +47,4 @@ var matcher = sanity.matchers.defined; | ||
if(matcher(source[key]) !== true) { | ||
failures.push({key: key, value: source[key]}); | ||
if(matcher(options.source[key]) !== true) { | ||
failures.push({key: key, value: options.source[key]}); | ||
} | ||
@@ -61,7 +67,7 @@ }); | ||
if(typeof cb === 'function') { | ||
if(typeof options.recover === 'function') { | ||
var failedKeys = failures.map(function(entry) { | ||
return entry.key; | ||
}); | ||
return cb(message !== '' ? message : null, failedKeys); | ||
return options.recover(message !== '' ? message : null, failedKeys); | ||
} | ||
@@ -72,7 +78,16 @@ | ||
} | ||
}, | ||
preach: function(insights, audience) { | ||
_.forEach(insights, function(commandment, word) { | ||
audience[word] = commandment; | ||
}); | ||
return audience; | ||
} | ||
}; | ||
exports.sanity = sanity; | ||
exports.check = sanity.check; | ||
exports.reporter = sanity.reporter; | ||
exports.matchers = sanity.matchers; | ||
exports.preach = sanity.preach; |
6739
76
141