diff --git a/types/convict/convict-tests.ts b/types/convict/convict-tests.ts index 3bc0b4565f..daa07c6875 100644 --- a/types/convict/convict-tests.ts +++ b/types/convict/convict-tests.ts @@ -1,48 +1,41 @@ - -/// - import convict = require('convict'); import validator = require('validator'); // define a schema // straight from the convict tests -const format : convict.Format = { +const format: convict.Format = { name: 'float-percent', - validate: function(val) { + validate(val) { if (val !== 0 && (!val || val > 1 || val < 0)) { throw new Error('must be a float between 0 and 1, inclusive'); } }, - coerce: function(val) { - return +( val); + coerce(val) { + return parseFloat(val); } }; - - - convict.addFormat(format); convict.addFormats({ - prime: { - validate: function(val) { - function isPrime(n: number) { - 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); + prime: { + validate(val) { + function isPrime(n: number) { + if (n <= 1) return false; // zero and one are not prime + for (let 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(val) { + return parseInt(val, 10); + } + } +}); - -var conf = convict({ +let conf = convict({ env: { doc: 'The applicaton environment.', format: ['production', 'development', 'test'], @@ -65,7 +58,11 @@ var conf = convict({ }, key: { doc: "API key", - format: (val: string) => validator.isUUID(val), + format: (val: string) => { + if (!validator.isUUID(val)) { + throw new Error('must be a valid UUID'); + } + }, default: '01527E56-8431-11E4-AF91-47B661C210CA' }, db: { @@ -81,7 +78,13 @@ var conf = convict({ default: 0, env: 'PORT', arg: 'port', - } + }, + password: { + doc: 'The database password.', + default: 'secret', + format: String, + sensitive: true, + }, }, primeNumber: { format: 'prime', @@ -91,14 +94,12 @@ var conf = convict({ format: 'float-percent', default: 0.5 }, - }); - // load environment dependent configuration -var env = conf.get('env'); -var dbip = conf.get('db.ip'); +let env = conf.get('env'); +let dbip = conf.get('db.ip'); conf.loadFile('./config/' + env + '.json'); conf.loadFile(['./configs/always.json', './configs/sometimes.json']); @@ -118,7 +119,7 @@ conf .validate({ allowed: 'warn' }) .toString(); -var port: number = conf.default('port'); +let port: number = conf.default('port'); if (conf.has('key')) { conf.set('the.awesome', true); diff --git a/types/convict/index.d.ts b/types/convict/index.d.ts index 82b22ba2db..183d91ed91 100644 --- a/types/convict/index.d.ts +++ b/types/convict/index.d.ts @@ -1,11 +1,11 @@ -// Type definitions for convict 3.0 +// Type definitions for convict 4.0 // Project: https://github.com/mozilla/node-convict -// Definitions by: Wim Looman , Vesa Poikajärvi +// Definitions by: Wim Looman +// Vesa Poikajärvi +// Eli Young // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped - declare namespace convict { - type ValidationMethod = 'strict' | 'warn'; interface ValidateOptions { @@ -23,8 +23,8 @@ declare namespace convict { interface Format { name?: string; - validate?: (val: any) => void; - coerce?: (val: any) => any; + validate?(val: any): void; + coerce?(val: any): any; } interface Schema { @@ -35,16 +35,18 @@ declare namespace convict { * From the implementation: * * format can be a: - * - predefine type, as seen below + * - predefined type, as seen below * - an array of enumerated values, e.g. ["production", "development", "testing"] * - built-in JavaScript type, i.e. Object, Array, String, Number, Boolean - * - or if omitted, the Object.prototype.toString.call of the default value + * - function that performs validation and throws an Error on failure * - * The docs also state that any function that validates is ok too + * If omitted, format will be set to the value of Object.prototype.toString.call + * for the default value */ - format?: string | Array | Function; + format?: string | any[] | ((val: any) => void); env?: string; arg?: string; + sensitive?: boolean; }; } @@ -78,17 +80,11 @@ declare namespace convict { */ load(conf: Object): Config; /** - * Loads and merges one JSON configuration file into config + * Loads and merges JSON configuration file(s) into config * * @return {Config} instance */ - loadFile(file: string): Config; - /** - * Loads and merges multiple JSON configuration files into config - * - * @return {Config} instance - */ - loadFile(files: string[]): Config; + loadFile(files: string | string[]): Config; /** * Validates config against the schema used to initialize it * diff --git a/types/convict/tslint.json b/types/convict/tslint.json new file mode 100644 index 0000000000..09f94cd344 --- /dev/null +++ b/types/convict/tslint.json @@ -0,0 +1,7 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + // ban-types needs to be disabled to support TypeScript <2.2 + "ban-types": false + } +}