Comparing version 0.2.0 to 0.2.1
@@ -49,2 +49,4 @@ /* | ||
var coerceTypes = {}; | ||
function validate (instance, schema, errors) { | ||
@@ -241,2 +243,5 @@ Object.keys(schema.properties).reduce(function(previousErrors, name) { | ||
if (typeof v === 'string') { | ||
if (coerceTypes.hasOwnProperty(format)) { | ||
return coerceTypes[format](v); | ||
} | ||
switch (format) { | ||
@@ -260,3 +265,3 @@ case 'port': | ||
module.exports = function convict(def) { | ||
var convict = function convict(def) { | ||
var rv = { | ||
@@ -352,3 +357,9 @@ toString: function() { | ||
rv._def = def; | ||
// If the definition is a string, | ||
// treat it as an external schema file. | ||
if (typeof def === 'string') { | ||
rv._def = cjson.load(def); | ||
} else { | ||
rv._def = def; | ||
} | ||
@@ -363,4 +374,4 @@ // build up current config from definition | ||
Object.keys(def).forEach(function(k) { | ||
normalizeSchema(k, def[k], rv._schema.properties, k, rv._env, rv._argv); | ||
Object.keys(rv._def).forEach(function(k) { | ||
normalizeSchema(k, rv._def[k], rv._schema.properties, k, rv._env, rv._argv); | ||
}); | ||
@@ -376,1 +387,21 @@ | ||
// Add a type with a validatino function | ||
// A coerce function is optional | ||
convict.addType = function(name, validator, coerce) { | ||
if (typeof validator !== 'function') { | ||
throw new Error('Validation function for ' + name + ' must be a function.'); | ||
} | ||
if (coerce && typeof coerce !== 'function') { | ||
throw new Error('Coerce function for ' + name + ' must be a function.'); | ||
} | ||
types[name] = validator; | ||
if (coerce) coerceTypes[name] = coerce; | ||
}; | ||
convict.addTypes = function(newTypes) { | ||
Object.keys(newTypes).forEach(function(type) { | ||
convict.addType(type, newTypes[type].validate, newTypes[type].coerce); | ||
}); | ||
}; | ||
module.exports = convict; |
@@ -5,3 +5,3 @@ { | ||
"description": "Unruly configuration management for nodejs", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"homepage": "https://github.com/lloyd/node-convict", | ||
@@ -8,0 +8,0 @@ "repository": { |
@@ -9,2 +9,20 @@ const should = require('should'); | ||
it('should parse a config specification', function() { | ||
convict.addTypes({ | ||
prime: { | ||
validate: function(val) { | ||
function isPrime(n) { | ||
if (n <= 1) return false; // zero and one are not prime | ||
for (var i=2; i*i <= n; i++) { | ||
if (n % i == 0) return false; | ||
} | ||
return true; | ||
} | ||
if (!isPrime(val)) throw new Error('must be a prime number'); | ||
}, | ||
coerce: function(val) { | ||
return parseInt(val, 10); | ||
} | ||
} | ||
}); | ||
conf = convict({ | ||
@@ -57,3 +75,7 @@ foo: { | ||
default: 'abcd' | ||
} | ||
}, | ||
primeNumber: { | ||
format: 'prime', | ||
default: 17 | ||
}, | ||
} | ||
@@ -67,2 +89,7 @@ }); | ||
it('should be invalid', function() { | ||
conf.set('foo.primeNumber', 16); | ||
(function() { conf.validate() }).should.throw(); | ||
}); | ||
describe('predefined formats', function() { | ||
@@ -69,0 +96,0 @@ it('should handle timestamp', function() { |
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
46207
42
1262