From 321e862d5a3618f2ce96de0b4cc4075f4e93fad9 Mon Sep 17 00:00:00 2001 From: Nimish Telang Date: Wed, 13 Jan 2016 14:48:03 +0000 Subject: [PATCH 1/4] Update convict definitions to add custom formats --- convict/convict-tests.ts | 43 +++++++++++++++++++++++++++++- convict/convict.d.ts | 57 ++++++++++++++++++++++++---------------- 2 files changed, 76 insertions(+), 24 deletions(-) diff --git a/convict/convict-tests.ts b/convict/convict-tests.ts index 6cde3b38c..54a570141 100644 --- a/convict/convict-tests.ts +++ b/convict/convict-tests.ts @@ -6,6 +6,39 @@ import validator = require('validator'); // define a schema +// straight from the convict tests +const format : convict.Format = { + name: 'float-percent', + validate: function(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); + } +}; + +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); + } + } + }); + + var conf = convict({ env: { doc: 'The applicaton environment.', @@ -46,7 +79,15 @@ var conf = convict({ env: 'PORT', arg: 'port', } - } + }, + primeNumber: { + format: 'prime', + default: 17 + }, + percentNumber: { + format: 'float-percent', + default: 0.5 + }, }); diff --git a/convict/convict.d.ts b/convict/convict.d.ts index 74ed10038..332441b20 100644 --- a/convict/convict.d.ts +++ b/convict/convict.d.ts @@ -4,30 +4,41 @@ // Definitions: https://github.com/borisyankov/DefinitelyTyped declare module "convict" { - function convict(schema: convict.Schema): convict.Config; + module convict { - module convict { - interface Schema { - [name: string]: convict.Schema | { - default: any; - doc?: string; - format?: any; - env?: string; - arg?: string; - }; - } + interface Format { + name?: string; + validate?: (val: any) => void; + coerce?: (val: any) => any; + } - interface Config { - get(name: string): any; - default(name: string): any; - has(name: string): boolean; - set(name: string, value: any): void; - load(conf: Object): void; - loadFile(file: string): void; - loadFile(files: string[]): void; - validate(): void; - } - } + interface Schema { + [name: string]: convict.Schema | { + default: any; + doc?: string; + format?: any; + env?: string; + arg?: string; + }; + } - export = convict; + interface Config { + get(name: string): any; + default(name: string): any; + has(name: string): boolean; + set(name: string, value: any): void; + load(conf: Object): void; + loadFile(file: string): void; + loadFile(files: string[]): void; + validate(): void; + } + } + interface convict { + addFormat(format: convict.Format): void; + addFormats(formats: { [name: string]: convict.Format }): void; + (config: convict.Schema): convict.Config; + } + var convict : convict; + export = convict; } + From 981c9d1112a5151c714aeec84e20ce0a92083256 Mon Sep 17 00:00:00 2001 From: Nimish Telang Date: Wed, 13 Jan 2016 17:18:43 +0000 Subject: [PATCH 2/4] Add missing methods to config --- convict/convict-tests.ts | 9 +++++++++ convict/convict.d.ts | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/convict/convict-tests.ts b/convict/convict-tests.ts index 54a570141..3bd64c592 100644 --- a/convict/convict-tests.ts +++ b/convict/convict-tests.ts @@ -19,6 +19,9 @@ const format : convict.Format = { } }; + + + convict.addFormat(format); convict.addFormats({ prime: { @@ -113,4 +116,10 @@ if (conf.has('key')) { } }); } + +conf.getSchema(); +conf.getProperties(); +conf.getSchemaString(); +conf.toString(); + // vim:et:sw=2:ts=2 diff --git a/convict/convict.d.ts b/convict/convict.d.ts index 332441b20..51c1f6a68 100644 --- a/convict/convict.d.ts +++ b/convict/convict.d.ts @@ -31,6 +31,28 @@ declare module "convict" { loadFile(file: string): void; loadFile(files: string[]): void; validate(): void; + /** + * Exports all the properties (that is the keys and their current values) as a {JSON} {Object} + * @returns {Object} A {JSON} compliant {Object} + */ + getProperties() : Object; + /** + * Exports the schema as a {JSON} {Object} + * @returns {Object} A {JSON} compliant {Object} + */ + getSchema() : Object; + + /** + * Exports all the properties (that is the keys and their current values) as a JSON string. + * @returns {String} a string representing this object + */ + toString() : string; + + /** + * Exports the schema as a JSON string. + * @returns {String} a string representing the schema of this {Config} + */ + getSchemaString() : string; } } interface convict { From 592d0403d180fc0fe79352999da27e288fdefa9c Mon Sep 17 00:00:00 2001 From: Nimish Telang Date: Wed, 13 Jan 2016 17:59:59 +0000 Subject: [PATCH 3/4] Refine format type in schema --- convict/convict.d.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/convict/convict.d.ts b/convict/convict.d.ts index 51c1f6a68..87788e096 100644 --- a/convict/convict.d.ts +++ b/convict/convict.d.ts @@ -16,7 +16,16 @@ declare module "convict" { [name: string]: convict.Schema | { default: any; doc?: string; - format?: any; + /** + * From the implementation: + * + * format can be a: + * - predefine 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 + */ + format?: string | Array | Function; env?: string; arg?: string; }; From 7581efb4ebce6d0d89569cfb2cfba20dfbadf7b1 Mon Sep 17 00:00:00 2001 From: Nimish Telang Date: Wed, 13 Jan 2016 18:00:50 +0000 Subject: [PATCH 4/4] Update docs --- convict/convict.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/convict/convict.d.ts b/convict/convict.d.ts index 87788e096..14ecae8f8 100644 --- a/convict/convict.d.ts +++ b/convict/convict.d.ts @@ -24,6 +24,8 @@ declare module "convict" { * - 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 + * + * The docs also state that any function that validates is ok too */ format?: string | Array | Function; env?: string;