Comparing version 0.0.3 to 1.0.0
var assert = require('assert'), | ||
fs = require('fs'); | ||
function check(config) { | ||
function Check(config) { | ||
this._config = config; | ||
@@ -12,3 +12,3 @@ this._errors = []; | ||
*/ | ||
check.prototype._getKey = function (keyname, testObj) { | ||
Check.prototype._getKey = function (keyname, testObj) { | ||
if (typeof keyname === 'string') { | ||
@@ -26,3 +26,3 @@ keyname = keyname.split("."); | ||
// Is it an object -- and does it have the key | ||
if (testObj !== Object(testObj) || !(key in testObj)) { | ||
if (testObj !== new Object(testObj) || !(key in testObj)) { | ||
return {error: 'Not found'}; | ||
@@ -43,3 +43,3 @@ } | ||
*/ | ||
check.prototype.has = function (keyname) { | ||
Check.prototype.has = function (keyname) { | ||
var res = this._getKey(keyname, this._config); | ||
@@ -61,3 +61,3 @@ | ||
*/ | ||
check.prototype.testFunction = function (key, testfunc) { | ||
Check.prototype.testFunction = function (key, testfunc) { | ||
var res = this._getKey(key, this._config); | ||
@@ -83,2 +83,24 @@ | ||
Check.prototype.optionalTestFunction = function (key, testfunc) { | ||
var res = this._getKey(key, this._config); | ||
if (res.error) { | ||
//this._errors.push("Missing key '" + key + "'."); | ||
return this; | ||
} | ||
try { | ||
var result = testfunc(key, res.value), | ||
resultType = Object.prototype.toString.call(result); | ||
if (resultType === '[object Error]' || resultType === '[object String]') { | ||
this._errors.push(result.message || result); | ||
} | ||
} catch (e) { | ||
this._errors.push(e.message); | ||
} | ||
return this; | ||
}; | ||
/* | ||
@@ -88,3 +110,3 @@ * Type checks | ||
['Array', 'Boolean', 'Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'].forEach(function (name) { | ||
check.prototype['is' + name] = function (keyname) { | ||
Check.prototype['has' + name] = function (keyname) { | ||
return this.testFunction(keyname, function (keyname, value) { | ||
@@ -97,23 +119,8 @@ if (Object.prototype.toString.call(value) !== '[object ' + name + ']') { | ||
}; | ||
}); | ||
check.prototype.isObject = function (keyname) { | ||
return this.testFunction(keyname, function (key, value) { | ||
return value !== Object(value) ? "Key '" + key + "' should be an object." : undefined; | ||
}); | ||
}; | ||
/* | ||
* File-system | ||
*/ | ||
['File', 'Directory', 'Socket', 'SymbolicLink'].forEach(function (name) { | ||
check.prototype['is'+name] = function (keyname) { | ||
return this.testFunction(keyname, function (key, value) { | ||
var stats = fs.lstatSync(value); | ||
if (!stats) { | ||
return "Key '"+key+"' → '"+value+"' cannot be stat()'ed."; | ||
Check.prototype['optional' + name] = function (keyname) { | ||
return this.optionalTestFunction(keyname, function (keyname, value) { | ||
if (Object.prototype.toString.call(value) !== '[object ' + name + ']') { | ||
return "Key '" + keyname + "' should be an " + name.toLowerCase() + "."; | ||
} | ||
if (!stats['is'+name]()) { | ||
return "Key '"+key+"' → '"+value+"' is not a "+name.toLowerCase()+"."; | ||
} | ||
return; | ||
@@ -124,14 +131,26 @@ }); | ||
Check.prototype.hasObject = function (keyname) { | ||
return this.testFunction(keyname, function (key, value) { | ||
return value !== new Object(value) ? "Key '" + key + "' should be an object." : undefined; | ||
}); | ||
}; | ||
Check.prototype.optionalObject = function (keyname) { | ||
return this.optionalTestFunction(keyname, function (key, value) { | ||
return value !== new Object(value) ? "Key '" + key + "' should be an object." : undefined; | ||
}); | ||
}; | ||
/* | ||
* Finalizers | ||
*/ | ||
check.prototype.errors = function () { | ||
Check.prototype.errors = function () { | ||
return this._errors; | ||
}; | ||
check.prototype.ok = function () { | ||
Check.prototype.ok = function () { | ||
return this._errors.length === 0; | ||
}; | ||
check.prototype.assert = function () { | ||
Check.prototype.assert = function () { | ||
if (this._errors.length !== 0) { | ||
@@ -149,3 +168,3 @@ throw new assert.AssertionError({ | ||
module.exports = function (config) { | ||
return new check(config); | ||
return new Check(config); | ||
}; |
@@ -5,3 +5,3 @@ { | ||
"description": "Check configurations completeness", | ||
"version": "0.0.3", | ||
"version": "1.0.0", | ||
"homepage": "http://github.com/msiebuhr/node-check/", | ||
@@ -15,8 +15,8 @@ "repository": { | ||
"devDependencies": { | ||
"jshint": "*", | ||
"vows": "*" | ||
}, | ||
"scripts": { | ||
"prepublish": "vows", | ||
"test": "vows" | ||
"test": "make" | ||
} | ||
} |
@@ -27,3 +27,3 @@ node-check | ||
.has('database.hostname') // Implicitly checks 'database' | ||
.isBoolean('database.debug') | ||
.hasBoolean('database.debug') | ||
.has('missingSetting') // Missing key - this will make check fail. | ||
@@ -58,28 +58,20 @@ .assert(); // Assert if anything isn't as required. | ||
.isArguments(key) | ||
.hasArguments(key) | ||
----------------- | ||
.isArray(key) | ||
.hasArray(key) | ||
------------- | ||
.isBoolean(key) | ||
.hasBoolean(key) | ||
--------------- | ||
.isDate(key) | ||
.hasDate(key) | ||
------------ | ||
.isDirectory(key) | ||
----------------- | ||
.isFile(key) | ||
------------ | ||
.isFunction(key) | ||
.hasFunction(key) | ||
---------------- | ||
.isNumber(key) | ||
.hasNumber(key) | ||
-------------- | ||
.isObject(key) | ||
.hasObject(key) | ||
-------------- | ||
.isRegExp(key) | ||
.hasRegExp(key) | ||
-------------- | ||
.isSocket(key) | ||
.hasString(key) | ||
-------------- | ||
.isString(key) | ||
-------------- | ||
.isSymbolicLink(key) | ||
-------------------- | ||
@@ -89,2 +81,26 @@ First checks if the key is present (as if running `.has(key)`), and if the | ||
.optionalArguments(key) | ||
----------------- | ||
.optionalArray(key) | ||
------------- | ||
.optionalBoolean(key) | ||
--------------- | ||
.optionalDate(key) | ||
------------ | ||
.optionalFunction(key) | ||
---------------- | ||
.optionalNumber(key) | ||
-------------- | ||
.optionalObject(key) | ||
-------------- | ||
.optionalRegExp(key) | ||
-------------- | ||
.optionalString(key) | ||
-------------- | ||
If the key is present, it checks if it is of the indicated type. | ||
Finalizers | ||
========== | ||
.assert() | ||
@@ -110,2 +126,7 @@ --------- | ||
See Also | ||
======== | ||
* [node-validator](https://github.com/chriso/node-validator) | ||
License | ||
@@ -112,0 +133,0 @@ ======= |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8230
7
134
0
132
2