Comparing version 1.5.0 to 2.0.0
@@ -6,4 +6,4 @@ /** | ||
*/ | ||
'use strict'; | ||
const deprecate = require('depd')('node-convict'); | ||
const json5 = require('json5'); | ||
@@ -14,3 +14,3 @@ const fs = require('fs'); | ||
const minimist = require('minimist'); | ||
const cloneDeep = require('lodash/cloneDeep'); | ||
const cloneDeep = require('lodash.clonedeep'); | ||
@@ -29,14 +29,42 @@ function assert(assertion, err_msg) { | ||
var types = { | ||
/** | ||
* Checks if x is a valid port | ||
* | ||
* @param {*} x | ||
* @returns {Boolean} | ||
*/ | ||
function isPort(x) { | ||
return Number.isInteger(x) && x >= 0 && x <= 65535; | ||
} | ||
/** | ||
* Checks if x is a named pipe | ||
* | ||
* @see https://msdn.microsoft.com/en-us/library/windows/desktop/aa365783(v=vs.85).aspx | ||
* @param {*} x | ||
* @returns {Boolean} | ||
*/ | ||
function isNamedPipe(x) { | ||
return String(x).includes('\\\\.\\pipe\\'); | ||
} | ||
const types = { | ||
'*': function() { }, | ||
int: function(x) { | ||
assert(validator.isInt(x), 'must be an integer'); | ||
assert(Number.isInteger(x), 'must be an integer'); | ||
}, | ||
nat: function(x) { | ||
assert(validator.isInt(x) && x >= 0, 'must be a positive integer'); | ||
assert(Number.isInteger(x) && x >= 0, 'must be a positive integer'); | ||
}, | ||
port: function(x) { | ||
assert(validator.isInt(x) && x >= 0 && x <= 65535, | ||
'Ports must be within range 0 - 65535'); | ||
assert(isPort(x), 'ports must be within range 0 - 65535'); | ||
}, | ||
named_pipe: function(x) { | ||
assert(isNamedPipe(x), 'must be a valid pipe'); | ||
}, | ||
named_pipe_or_port: function(x) { | ||
if (!isNamedPipe(x)) { | ||
assert(isPort(x), 'must be a pipe or a number within range 0 - 65535'); | ||
} | ||
}, | ||
url: function(x) { | ||
@@ -52,4 +80,4 @@ assert(validator.isURL(x), 'must be a URL'); | ||
duration: function(x) { | ||
var err_msg = 'must be a positive integer or human readable string (e.g. 3000, "5 days")'; | ||
if (validator.isInt(x)) { | ||
let err_msg = 'must be a positive integer or human readable string (e.g. 3000, "5 days")'; | ||
if (Number.isInteger(x)) { | ||
assert(x >= 0, err_msg); | ||
@@ -61,3 +89,3 @@ } else { | ||
timestamp: function(x) { | ||
assert(validator.isInt(x) && x >= 0, 'must be a positive integer'); | ||
assert(Number.isInteger(x) && x >= 0, 'must be a positive integer'); | ||
} | ||
@@ -68,7 +96,7 @@ }; | ||
var converters = {}; | ||
const converters = {}; | ||
function validate(instance, schema, errors,strictValidation) { | ||
Object.keys(instance).reduce(function(previousErrors, name) { | ||
var p = schema.properties[name]; | ||
let p = schema.properties[name]; | ||
if (strictValidation && !p){ | ||
@@ -81,3 +109,3 @@ previousErrors.push(new Error("configuration param '"+name+"' not declared in the schema")); | ||
if (p.properties) { | ||
var kids = instance[name] || {}; | ||
let kids = instance[name] || {}; | ||
validate(kids, p, previousErrors,strictValidation); | ||
@@ -104,3 +132,3 @@ } else if (! (typeof p.default === 'undefined' && | ||
var BUILT_INS_BY_NAME = { | ||
const BUILT_INS_BY_NAME = { | ||
'Object': Object, | ||
@@ -113,4 +141,4 @@ 'Array': Array, | ||
}; | ||
var BUILT_IN_NAMES = Object.keys(BUILT_INS_BY_NAME); | ||
var BUILT_INS = BUILT_IN_NAMES.map(function(name) { | ||
const BUILT_IN_NAMES = Object.keys(BUILT_INS_BY_NAME); | ||
const BUILT_INS = BUILT_IN_NAMES.map(function(name) { | ||
return BUILT_INS_BY_NAME[name]; | ||
@@ -135,4 +163,3 @@ }); | ||
var o; | ||
let o; | ||
if (typeof node === 'object') { | ||
@@ -160,4 +187,4 @@ o = Object.create(node); | ||
// store original format function | ||
var format = o.format; | ||
var newFormat; | ||
let format = o.format; | ||
let newFormat; | ||
@@ -167,3 +194,3 @@ if (BUILT_INS.indexOf(format) >= 0 || BUILT_IN_NAMES.indexOf(format) >= 0) { | ||
// assert that the value is of that type | ||
var Format = typeof format === 'string' ? BUILT_INS_BY_NAME[format] : format; | ||
let Format = typeof format === 'string' ? BUILT_INS_BY_NAME[format] : format; | ||
newFormat = function(x) { | ||
@@ -200,3 +227,3 @@ assert(Object.prototype.toString.call(x) == | ||
// default format is the typeof the default value | ||
var type = Object.prototype.toString.call(o.default); | ||
let type = Object.prototype.toString.call(o.default); | ||
newFormat = function(x) { | ||
@@ -229,3 +256,3 @@ assert(Object.prototype.toString.call(x) == type, | ||
Object.keys(o._env).forEach(function(envStr) { | ||
var k = o._env[envStr]; | ||
let k = o._env[envStr]; | ||
if (process.env[envStr]) { | ||
@@ -238,5 +265,5 @@ o.set(k, process.env[envStr]); | ||
function importArguments(o) { | ||
var argv = minimist(process.argv.slice(2)); | ||
let argv = minimist(process.argv.slice(2)); | ||
Object.keys(o._argv).forEach(function(argStr) { | ||
var k = o._argv[argStr]; | ||
let k = o._argv[argStr]; | ||
if (argv[argStr]) { | ||
@@ -250,5 +277,5 @@ o.set(k, argv[argStr]); | ||
Object.keys(schema.properties).forEach(function(name) { | ||
var p = schema.properties[name]; | ||
let p = schema.properties[name]; | ||
if (p.properties) { | ||
var kids = c[name] || {}; | ||
let kids = c[name] || {}; | ||
addDefaultValues(p, kids, instance); | ||
@@ -277,6 +304,6 @@ if (Object.keys(kids).length) c[name] = kids; | ||
function traverseSchema(schema, path) { | ||
var ar = path.split('.'); | ||
var o = schema; | ||
let ar = path.split('.'); | ||
let o = schema; | ||
while (ar.length > 0) { | ||
var k = ar.shift(); | ||
let k = ar.shift(); | ||
if (o && o.properties && o.properties[k]) { | ||
@@ -294,3 +321,3 @@ o = o.properties[k]; | ||
function getFormat(schema, path) { | ||
var o = traverseSchema(schema, path); | ||
let o = traverseSchema(schema, path); | ||
if (o == null) return null; | ||
@@ -304,3 +331,3 @@ if (typeof o.format === 'string') return o.format; | ||
// magic coerceing | ||
var format = getFormat(schema, k); | ||
let format = getFormat(schema, k); | ||
@@ -316,2 +343,3 @@ if (typeof v === 'string') { | ||
case 'int': v = parseInt(v, 10); break; | ||
case 'named_pipe_or_port': v = isNamedPipe(v) ? v : parseInt(v, 10); break; | ||
case 'number': v = parseFloat(v); break; | ||
@@ -323,4 +351,4 @@ case 'boolean': v = ((v === 'false') ? false : true); break; | ||
case 'timestamp': v = moment(v).valueOf(); break; | ||
case 'duration': | ||
var split = v.split(' '); | ||
case 'duration': { | ||
let split = v.split(' '); | ||
if (split.length == 1) { | ||
@@ -335,2 +363,3 @@ // It must be an integer in string form. | ||
break; | ||
} | ||
default: | ||
@@ -353,8 +382,8 @@ // TODO: Should we throw an exception here? | ||
*/ | ||
var convict = function convict(def) { | ||
let convict = function convict(def) { | ||
function walk(obj, path) { | ||
if (path) { | ||
var ar = path.split('.'); | ||
let ar = path.split('.'); | ||
while (ar.length) { | ||
var k = ar.shift(); | ||
let k = ar.shift(); | ||
if (k in obj) { | ||
@@ -373,3 +402,3 @@ obj = obj[k]; | ||
// more meaningful. | ||
var rv = { | ||
let rv = { | ||
/** | ||
@@ -381,5 +410,2 @@ * Exports all the properties (that is the keys and their current values) as JSON | ||
}, | ||
root: deprecate.function(function() { | ||
return this.getProperties(); | ||
}, 'Use "getProperties" method instead'), | ||
@@ -407,5 +433,2 @@ /** | ||
}, | ||
toSchemaString: deprecate.function(function() { | ||
return this.getSchemaString(); | ||
}, 'Use "getSchemaString" method instead'), | ||
@@ -417,3 +440,3 @@ /** | ||
get: function(path) { | ||
var o = walk(this._instance, path); | ||
let o = walk(this._instance, path); | ||
return typeof o !== 'undefined' ? | ||
@@ -432,3 +455,3 @@ cloneDeep(o) : | ||
path = (path.split('.').join('.properties.')) + '.default'; | ||
var o = walk(this._schema.properties, path); | ||
let o = walk(this._schema.properties, path); | ||
return typeof o !== 'undefined' ? | ||
@@ -451,3 +474,3 @@ cloneDeep(o) : | ||
try { | ||
var r = this.get(path); | ||
let r = this.get(path); | ||
// values that are set but undefined return false | ||
@@ -468,4 +491,4 @@ return typeof r !== 'undefined'; | ||
var ar = k.split('.'); | ||
var o = this._instance; | ||
let ar = k.split('.'); | ||
let o = this._instance; | ||
while (ar.length > 1) { | ||
@@ -495,3 +518,3 @@ k = ar.shift(); | ||
loadFile: function(paths) { | ||
var self = this; | ||
let self = this; | ||
if (!Array.isArray(paths)) paths = [paths]; | ||
@@ -513,12 +536,12 @@ paths.forEach(function(path) { | ||
options.strict = options.strict || false; | ||
var errors = validate(this._instance, this._schema, [],options.strict); | ||
let errors = validate(this._instance, this._schema, [],options.strict); | ||
if (errors.length) { | ||
var errBuf = ''; | ||
let errBuf = ''; | ||
for (var i = 0; i < errors.length; i++) { | ||
for (let i = 0; i < errors.length; i++) { | ||
if (errBuf.length) errBuf += '\n'; | ||
var e = errors[i]; | ||
let e = errors[i]; | ||
@@ -525,0 +548,0 @@ if (e.fullName) { |
{ | ||
"name": "convict", | ||
"version": "1.5.0", | ||
"version": "2.0.0", | ||
"dependencies": { | ||
"depd": { | ||
"version": "1.1.0", | ||
"from": "depd@1.1.0", | ||
"resolved": "https://registry.npmjs.org/depd/-/depd-1.1.0.tgz" | ||
}, | ||
"json5": { | ||
"version": "0.5.0", | ||
"from": "json5@0.5.0", | ||
"resolved": "https://registry.npmjs.org/json5/-/json5-0.5.0.tgz" | ||
"version": "0.5.1", | ||
"from": "json5@0.5.1", | ||
"resolved": "https://registry.npmjs.org/json5/-/json5-0.5.1.tgz" | ||
}, | ||
"lodash": { | ||
"version": "4.16.2", | ||
"from": "lodash@4.16.2", | ||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.16.2.tgz" | ||
"lodash.clonedeep": { | ||
"version": "4.5.0", | ||
"from": "lodash.clonedeep@4.5.0", | ||
"resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz" | ||
}, | ||
@@ -26,10 +21,10 @@ "minimist": { | ||
"moment": { | ||
"version": "2.12.0", | ||
"from": "moment@2.12.0", | ||
"resolved": "https://registry.npmjs.org/moment/-/moment-2.12.0.tgz" | ||
"version": "2.17.1", | ||
"from": "moment@2.17.1", | ||
"resolved": "https://registry.npmjs.org/moment/-/moment-2.17.1.tgz" | ||
}, | ||
"validator": { | ||
"version": "4.6.1", | ||
"from": "validator@4.6.1", | ||
"resolved": "https://registry.npmjs.org/validator/-/validator-4.6.1.tgz" | ||
"version": "6.2.0", | ||
"from": "validator@6.2.0", | ||
"resolved": "https://registry.npmjs.org/validator/-/validator-6.2.0.tgz" | ||
}, | ||
@@ -36,0 +31,0 @@ "varify": { |
@@ -13,3 +13,3 @@ { | ||
], | ||
"version": "1.5.0", | ||
"version": "2.0.0", | ||
"license": "Apache-2.0", | ||
@@ -23,3 +23,3 @@ "homepage": "https://github.com/mozilla/node-convict", | ||
"engines": { | ||
"node": ">=0.4.7" | ||
"node": ">=4" | ||
}, | ||
@@ -46,16 +46,15 @@ "scripts": { | ||
"dependencies": { | ||
"depd": "1.1.0", | ||
"json5": "0.5.0", | ||
"lodash": "4.16.2", | ||
"json5": "0.5.1", | ||
"lodash.clonedeep": "4.5.0", | ||
"minimist": "1.2.0", | ||
"moment": "2.12.0", | ||
"validator": "4.6.1" | ||
"moment": "2.17.1", | ||
"validator": "6.2.0" | ||
}, | ||
"devDependencies": { | ||
"coveralls": "2.11.9", | ||
"eslint": "2.5.3", | ||
"istanbul": "0.4.2", | ||
"mocha": "2.4.5", | ||
"coveralls": "2.11.15", | ||
"eslint": "3.12.2", | ||
"istanbul": "0.4.5", | ||
"mocha": "3.2.0", | ||
"mocha-lcov-reporter": "1.2.0", | ||
"must": "0.13.1", | ||
"must": "0.13.2", | ||
"obj_diff": "0.3.0" | ||
@@ -62,0 +61,0 @@ }, |
@@ -171,2 +171,4 @@ # Node-convict | ||
* `port` | ||
* `named_pipe` | ||
* `named_pipe_or_port` | ||
* `url` | ||
@@ -235,37 +237,3 @@ * `email` | ||
The second argument to `coerce` is the `config` object, this can be used to implement placeholders and other advanced functionality: | ||
```javascript | ||
convict.addFormat({ | ||
name: "placeholder", | ||
validate: function(val) { | ||
/* validate proper path here */ | ||
}, | ||
coerce: function(val, config) { | ||
return val.replace(/\$\{([\w\.]+)}/g, function(v,m) { return config.get(m); }); | ||
} | ||
}); | ||
var conf = convict({ | ||
env: { | ||
format: ['production', 'development'], | ||
default: 'development', | ||
env: 'NODE_ENV', | ||
doc: 'The environment that we\'re running in.' | ||
}, | ||
configPath: { | ||
format: 'placeholder', | ||
default: '/path/to/config', | ||
doc: 'Path to configuration files.' | ||
}, | ||
config: { | ||
format: 'placeholder', | ||
default: '${configPath}/${env}.json', | ||
doc: 'Path to configuration file.' | ||
} | ||
}); | ||
conf.get('config'); /* "/path/to/config/development.json" */ | ||
``` | ||
### Precendence order | ||
@@ -272,0 +240,0 @@ |
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
6
575
42603
375
+ Addedlodash.clonedeep@4.5.0
+ Addedjson5@0.5.1(transitive)
+ Addedlodash.clonedeep@4.5.0(transitive)
+ Addedmoment@2.17.1(transitive)
+ Addedvalidator@6.2.0(transitive)
- Removeddepd@1.1.0
- Removedlodash@4.16.2
- Removeddepd@1.1.0(transitive)
- Removedjson5@0.5.0(transitive)
- Removedlodash@4.16.2(transitive)
- Removedmoment@2.12.0(transitive)
- Removedvalidator@4.6.1(transitive)
Updatedjson5@0.5.1
Updatedmoment@2.17.1
Updatedvalidator@6.2.0