From 80e0d7f7744522328a5433f41b0ac98ae3f25128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vesa=20Poikaj=C3=A4rvi?= Date: Tue, 23 May 2017 13:23:53 +0300 Subject: [PATCH 1/3] [convict] 3.0.0 deprecated option strict and added option allowed --- types/convict/convict-tests.ts | 2 ++ types/convict/index.d.ts | 26 +++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/types/convict/convict-tests.ts b/types/convict/convict-tests.ts index a61c27bdad..667652b829 100644 --- a/types/convict/convict-tests.ts +++ b/types/convict/convict-tests.ts @@ -105,6 +105,8 @@ conf.loadFile(['./configs/always.json', './configs/sometimes.json']); // perform validation conf.validate({ strict: true }); +conf.validate({ allowed: 'strict' }); +conf.validate({ allowed: 'warn' }); var port: number = conf.default('port'); diff --git a/types/convict/index.d.ts b/types/convict/index.d.ts index 981bc8148b..d9f12c29dd 100644 --- a/types/convict/index.d.ts +++ b/types/convict/index.d.ts @@ -1,11 +1,26 @@ -// Type definitions for node-convict v0.6.0 +// Type definitions for node-convict v3.0.0 // Project: https://github.com/mozilla/node-convict -// Definitions by: Wim Looman +// Definitions by: Wim Looman , Vesa Poikajärvi // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare namespace convict { + type ValidationMethod = 'strict' | 'warn'; + + interface ValidateOptions { + /** + * If set to warn, any properties specified in config files that are not declared in + * the schema will print a warning. This is the default behavior. If set to strict, + * any properties specified in config files that are not declared in the schema will + * throw errors. This is to ensure that the schema and the config files are in sync. + */ + allowed?: ValidationMethod; + + /** @deprecated use allowed instead */ + strict?: boolean; + } + interface Format { name?: string; validate?: (val: any) => void; @@ -41,7 +56,12 @@ declare namespace convict { load(conf: Object): void; loadFile(file: string): void; loadFile(files: string[]): void; - validate(options?: { strict?: boolean }): void; + /** + * Validates config against the schema used to initialize it + * + * @param options + */ + validate(options?: ValidateOptions): void; /** * Exports all the properties (that is the keys and their current values) as a {JSON} {Object} * @returns {Object} A {JSON} compliant {Object} From b475ab8f372511eaf5d97bf03021c7f5bbe48ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vesa=20Poikaj=C3=A4rvi?= Date: Tue, 23 May 2017 13:36:46 +0300 Subject: [PATCH 2/3] [convict] Many Config methods are chainable --- types/convict/convict-tests.ts | 10 ++++++++ types/convict/index.d.ts | 43 ++++++++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/types/convict/convict-tests.ts b/types/convict/convict-tests.ts index 667652b829..10f5813185 100644 --- a/types/convict/convict-tests.ts +++ b/types/convict/convict-tests.ts @@ -108,6 +108,16 @@ conf.validate({ strict: true }); conf.validate({ allowed: 'strict' }); conf.validate({ allowed: 'warn' }); +// Chaining + +conf + .loadFile(['./configs/always.json', './configs/sometimes.json']) + .loadFile('./config/' + env + '.json') + .load({ jsonKey: 'jsonValue' }) + .set('key', 'value') + .validate( {allowed: 'warn'} ) + .toString(); + var port: number = conf.default('port'); if (conf.has('key')) { diff --git a/types/convict/index.d.ts b/types/convict/index.d.ts index d9f12c29dd..ebe5d21490 100644 --- a/types/convict/index.d.ts +++ b/types/convict/index.d.ts @@ -49,19 +49,52 @@ declare namespace convict { } interface Config { + /** + * @returns the current value of the name property. name can use dot + * notation to reference nested values + */ get(name: string): any; + /** + * @returns the default value of the name property. name can use dot + * notation to reference nested values + */ default(name: string): any; + /** + * @returns true if the property name is defined, or false otherwise + */ has(name: string): boolean; - set(name: string, value: any): void; - load(conf: Object): void; - loadFile(file: string): void; - loadFile(files: string[]): void; + /** + * Sets the value of name to value. name can use dot notation to reference + * nested values, e.g. "database.port". If objects in the chain don't yet + * exist, they will be initialized to empty objects + * + * @return {Config} instance + */ + set(name: string, value: any): Config; + /** + * Loads and merges a JavaScript object into config + * + * @return {Config} instance + */ + load(conf: Object): Config; + /** + * Loads and merges one JSON configuration file 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; /** * Validates config against the schema used to initialize it * * @param options */ - validate(options?: ValidateOptions): void; + validate(options?: ValidateOptions): Config; /** * Exports all the properties (that is the keys and their current values) as a {JSON} {Object} * @returns {Object} A {JSON} compliant {Object} From df2c87326f2d6cafd2f201c0ea038e1e3744404b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vesa=20Poikaj=C3=A4rvi?= Date: Tue, 23 May 2017 13:55:29 +0300 Subject: [PATCH 3/3] [convict] incorrect whitespace fixed from test --- types/convict/convict-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/convict/convict-tests.ts b/types/convict/convict-tests.ts index 10f5813185..0451954ca1 100644 --- a/types/convict/convict-tests.ts +++ b/types/convict/convict-tests.ts @@ -115,7 +115,7 @@ conf .loadFile('./config/' + env + '.json') .load({ jsonKey: 'jsonValue' }) .set('key', 'value') - .validate( {allowed: 'warn'} ) + .validate({ allowed: 'warn' }) .toString(); var port: number = conf.default('port');